前台HTML代码:
1 <html xmlns="http://www.w3.org/1999/xhtml" >
2 <head runat="server">
3 <title>DropWownList联动</title>
4 <script type="text/javascript">
5 function CallServer(arg)
6 {
7 var classId=arg.options[arg.selectedIndex].value;
8 <% =GetCallBackString %>
9
10 }
11 function ReceiveServerData(timeMessage)
12 {
13 //处理服务器返回的字符串,填充dropdownList2
14 var oObject = eval('(' + timeMessage + ')');//由于从服务器返回的是json数据格式,在此用eval()函数来解析成js的对象
15 var ddl=document.getElementById("DropDownList2");
16 ddl.options.length=0;
17 for(var i=0;i<oObject.length;i++)
18 {
19 var news=oObject[i];
20 var opt=new Option(news.title,news.newsId);
21 ddl.add(opt);
22
23 }
24 }
25 </script>
26 </head>
27 <body>
28 <form id="form1" runat="server">
29 <div>
30 <asp:DropDownList ID="DropDownList1" runat="server" onchange="CallServer(this);">
31 </asp:DropDownList>
32 <asp:DropDownList ID="DropDownList2" runat="server">
33 </asp:DropDownList>
34 </div>
35
36 </form>
37 </body>
38 </html>
2 <head runat="server">
3 <title>DropWownList联动</title>
4 <script type="text/javascript">
5 function CallServer(arg)
6 {
7 var classId=arg.options[arg.selectedIndex].value;
8 <% =GetCallBackString %>
9
10 }
11 function ReceiveServerData(timeMessage)
12 {
13 //处理服务器返回的字符串,填充dropdownList2
14 var oObject = eval('(' + timeMessage + ')');//由于从服务器返回的是json数据格式,在此用eval()函数来解析成js的对象
15 var ddl=document.getElementById("DropDownList2");
16 ddl.options.length=0;
17 for(var i=0;i<oObject.length;i++)
18 {
19 var news=oObject[i];
20 var opt=new Option(news.title,news.newsId);
21 ddl.add(opt);
22
23 }
24 }
25 </script>
26 </head>
27 <body>
28 <form id="form1" runat="server">
29 <div>
30 <asp:DropDownList ID="DropDownList1" runat="server" onchange="CallServer(this);">
31 </asp:DropDownList>
32 <asp:DropDownList ID="DropDownList2" runat="server">
33 </asp:DropDownList>
34 </div>
35
36 </form>
37 </body>
38 </html>
C#代码:
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using HnDB;
12 using System.Text;
13
14 public partial class dropDownList : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
15 {
16 IHnDB sqlDB;
17 DataSet ds = new DataSet();
18 string sqlstr = "";
19 public string GetCallBackString;
20 protected void Page_Load(object sender, EventArgs e)
21 {
22 ClientScriptManager cm = Page.ClientScript;//管理客户端JS的类
23 //下面这个函数获取一个对客户端回调的引用
24 //参数说明
25 //1.this 表示当前页面对象,它可以是page类,也可以是control,但是都必须实现System.Web.UI.ICallbackEventHandler接口
26 //2."classId"从客户端传来的参数
27 //3.客户端回调函数名称
28 //4.返回值为字符串,其本质为一个js函数供客户端调用
29 GetCallBackString = cm.GetCallbackEventReference(this, "classId", "ReceiveServerData", "");
30
31 //以下初始界面,绑定dropdownList
32 sqlstr = "select classid,classname from dbo.NewsClass";
33 string assemblyName = ConfigurationManager.AppSettings["assemblyName"].ToString();
34 string className = ConfigurationManager.AppSettings["className"].ToString();
35 string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
36 sqlDB = (new DbFactory(assemblyName, className, conStr)).HnDB;
37 ds=sqlDB.GetDataSet(sqlstr);
38 DropDownList1.DataSource = ds.Tables[0].DefaultView;
39 DropDownList1.DataTextField = "classname";
40 DropDownList1.DataValueField = "classid";
41 DropDownList1.DataBind();
42 ListItem list = new ListItem("-未选择-", "0");
43 DropDownList1.Items.Insert(0, list);
44
45
46 }
47 private string returnStr;
48 //接收客户端的数据进行处理
49 public void RaiseCallbackEvent(string eventArgument)
50 {
51 sqlstr = "select NewsID,title from dbo.News WHERE CLASSID=" + eventArgument;
52 ds = sqlDB.GetDataSet(sqlstr);
53 StringBuilder sb=new StringBuilder();
54 sb.Append("[");
55 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
56 {
57 sb.Append("{");
58 sb.Append("newsId:"+ds.Tables[0].Rows[i]["NewsID"].ToString()+",");
59 sb.Append("title:\"" + ds.Tables[0].Rows[i]["title"].ToString()+"\"");
60 if (i == ds.Tables[0].Rows.Count - 1)
61 sb.Append("}");
62 else
63 sb.Append("},");
64 }
65 sb.Append("]");
66 returnStr = sb.ToString();
67 return;
68 }
69
70 //将处理的数据返回给客户端调用
71 public string GetCallbackResult()
72 {
73 return returnStr;
74 }
75
76 }
77
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using HnDB;
12 using System.Text;
13
14 public partial class dropDownList : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
15 {
16 IHnDB sqlDB;
17 DataSet ds = new DataSet();
18 string sqlstr = "";
19 public string GetCallBackString;
20 protected void Page_Load(object sender, EventArgs e)
21 {
22 ClientScriptManager cm = Page.ClientScript;//管理客户端JS的类
23 //下面这个函数获取一个对客户端回调的引用
24 //参数说明
25 //1.this 表示当前页面对象,它可以是page类,也可以是control,但是都必须实现System.Web.UI.ICallbackEventHandler接口
26 //2."classId"从客户端传来的参数
27 //3.客户端回调函数名称
28 //4.返回值为字符串,其本质为一个js函数供客户端调用
29 GetCallBackString = cm.GetCallbackEventReference(this, "classId", "ReceiveServerData", "");
30
31 //以下初始界面,绑定dropdownList
32 sqlstr = "select classid,classname from dbo.NewsClass";
33 string assemblyName = ConfigurationManager.AppSettings["assemblyName"].ToString();
34 string className = ConfigurationManager.AppSettings["className"].ToString();
35 string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
36 sqlDB = (new DbFactory(assemblyName, className, conStr)).HnDB;
37 ds=sqlDB.GetDataSet(sqlstr);
38 DropDownList1.DataSource = ds.Tables[0].DefaultView;
39 DropDownList1.DataTextField = "classname";
40 DropDownList1.DataValueField = "classid";
41 DropDownList1.DataBind();
42 ListItem list = new ListItem("-未选择-", "0");
43 DropDownList1.Items.Insert(0, list);
44
45
46 }
47 private string returnStr;
48 //接收客户端的数据进行处理
49 public void RaiseCallbackEvent(string eventArgument)
50 {
51 sqlstr = "select NewsID,title from dbo.News WHERE CLASSID=" + eventArgument;
52 ds = sqlDB.GetDataSet(sqlstr);
53 StringBuilder sb=new StringBuilder();
54 sb.Append("[");
55 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
56 {
57 sb.Append("{");
58 sb.Append("newsId:"+ds.Tables[0].Rows[i]["NewsID"].ToString()+",");
59 sb.Append("title:\"" + ds.Tables[0].Rows[i]["title"].ToString()+"\"");
60 if (i == ds.Tables[0].Rows.Count - 1)
61 sb.Append("}");
62 else
63 sb.Append("},");
64 }
65 sb.Append("]");
66 returnStr = sb.ToString();
67 return;
68 }
69
70 //将处理的数据返回给客户端调用
71 public string GetCallbackResult()
72 {
73 return returnStr;
74 }
75
76 }
77