
<%...@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="mytest.WebForm1"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
<script>...
vararry=newArray();
<%
for(inti=0;i<cdt.Rows.Count;i++)
...{
%>
arry[<%=i%>]=newArray("<%=cdt.Rows[i]["d_id"]%>","<%=cdt.Rows[i]["subname"]%>","<%=cdt.Rows[i]["sub_id"]%>");//注意这里的Array的使用,里面有三个下标,第一个下标索引为0
<%
}
%>
functionbindsub(value)
...{
document.getElementById("dlsmall").length=0;
for(vari=0;i<arry.length;i++)
...{
if(arry[i][0]==value)//重点:用于判断是不是选中的大类所对应的小类
...{
document.getElementById("dlsmall").options.add(newOption(arry[i][1],arry[i][2]));//arry[i][1]指arry数组里的下标为1的元素值
}
}
}
</script>
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<FONTface="宋体">
<asp:DropDownListid="dlbig"style="Z-INDEX:101;LEFT:296px;POSITION:absolute;TOP:192px"runat="server"></asp:DropDownList>
<asp:DropDownListid="dlsmall"style="Z-INDEX:102;LEFT:440px;POSITION:absolute;TOP:192px"runat="server"></asp:DropDownList>
<asp:Buttonid="Button1"style="Z-INDEX:103;LEFT:376px;POSITION:absolute;TOP:264px"runat="server"
Text="Button"></asp:Button>
<asp:Labelid="Label1"style="Z-INDEX:104;LEFT:296px;POSITION:absolute;TOP:80px"runat="server"
Width="312px"ForeColor="Red">Label</asp:Label></FONT>
</form>
</body>
</HTML>
cs代码如下:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
namespacemytest
...{
/**////<summary>
///WebForm1的摘要说明。
///</summary>
publicclassWebForm1:System.Web.UI.Page
...{
privatereadonlystringconnstr=ConfigurationSettings.AppSettings["connstr"].ToString();
protectedSystem.Web.UI.WebControls.DropDownListdlbig;
protectedSystem.Web.UI.WebControls.DropDownListdlsmall;
protectedSystem.Web.UI.WebControls.ButtonButton1;
protectedSystem.Web.UI.WebControls.LabelLabel1;
protectedDataTablecdt;//这里是html页面中要用到的datatable对象,所以必须将其设为公共的
privatevoidPage_Load(objectsender,System.EventArgse)
...{
if(!Page.IsPostBack)
...{
cdt=GetTB("select*fromsubunionaddress");
this.dlbig.Attributes.Add("onchange","bindsub(this.value)");
bindbig();
}
}
privatevoidbindbig()
...{
stringsql="select*fromunionaddress";
DataTabledt=GetTB(sql);
this.dlbig.DataTextField="name";
this.dlbig.DataValueField="d_id";
this.dlbig.DataSource=dt;
this.dlbig.DataBind();
}
privateDataTableGetTB(stringsql)
...{
SqlConnectionmyconn=newSqlConnection(connstr);
SqlDataAdapteradpt=newSqlDataAdapter(sql,myconn);
DataSetds=newDataSet();
adpt.Fill(ds);
return(DataTable)ds.Tables[0];
}

Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
...{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
...{
this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidButton1_Click(objectsender,System.EventArgse)
...{
stringbig=dlbig.SelectedValue.ToString();
stringnobig=dlsmall.SelectedValue.ToString();
stringsmall=Request.Form["dlsmall"].ToString();
//this.Label1.Text="当前你选定的是"+big.ToString()+"地区,"+small.ToString()+"市";
}
}
}
本文介绍了一个使用ASP.NET实现的DropDownList控件联动选择功能的例子。通过C#后端代码,从数据库获取数据并填充两个下拉列表,实现根据大类自动更新小类选项的功能。
1388

被折叠的 条评论
为什么被折叠?



