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

<% ... @Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="mytest.WebForm1" %>
<! DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN" >
< HTML >
< HEAD >
< title > WebForm1 </ title >
< meta name ="GENERATOR" Content ="MicrosoftVisualStudio.NET7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="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 >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< FONT face ="宋体" >
< asp:DropDownList id ="dlbig" style ="Z-INDEX:101;LEFT:296px;POSITION:absolute;TOP:192px" runat ="server" ></ asp:DropDownList >
< asp:DropDownList id ="dlsmall" style ="Z-INDEX:102;LEFT:440px;POSITION:absolute;TOP:192px" runat ="server" ></ asp:DropDownList >
< asp:Button id ="Button1" style ="Z-INDEX:103;LEFT:376px;POSITION:absolute;TOP:264px" runat ="server"
Text
="Button" ></ asp:Button >
< asp:Label id ="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代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;

namespace mytest
... {
/**////<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()+"市";
}

}

}

你可能感兴趣的:(JavaScript)