用.net生成javascript后,再实现二层菜单的级联

本文介绍了一个使用ASP.NET实现的DropDownList控件联动选择功能的例子。通过C#后端代码,从数据库获取数据并填充两个下拉列表,实现根据大类自动更新小类选项的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%...@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()+"市";
}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值