以下代码是实现动态创建ASPxComboBox控件,以及在运行时给控件赋Callback Event, 从而实现两个ASPxComboBox控件数据联动的效果(populating on the fly), 静态效果的实现可参照:http://demos.devexpress.com/ASPxEditorsDemos/ASPxComboBox/ClientAPI.aspx
代码
1
protected
void
Page_Load(
object
sender, EventArgs e)
2 {
3 ASPxComboBox cbxMain = new ASPxComboBox();
4 cbxMain.ID = " cbx_Main " ;
5 cbxMain.DataSource = SQLHELPER.GetDataSet( " select eat, name from main_table " );
6 cbxMain.ClientInstanceName = " cbx_Main " ;
7 cbxMain.TextField = " name " ;
8 cbxMain.ValueField = " eat " ;
9 cbxMain.DataBind();
10 cbxMain.ClientSideEvents.SelectedIndexChanged = " function(s, e) { OnCountryChanged(s); } " ;
11
12 ASPxComboBox cbxSub = new ASPxComboBox();
13 cbxSub.ID = " cbx_Sub " ;
14 cbxSub.ClientInstanceName = " cbx_Sub " ;
15 cbxSub.Callback += new DevExpress.Web.ASPxClasses.CallbackEventHandlerBase(cbxSub_Callback);
16
17 // 注册脚本
18 string js = " <script language=\ " javascript\ " >function OnCountryChanged(cbx_Main) { " ;
19 js += " cbx_Sub.PerformCallback(cbx_Main.GetValue().toString()); " ;
20 js += " }</script> " ;
21
22 ClientScript.RegisterClientScriptBlock( this .GetType(), " OnCountryChanged " , js);
23 pnl_DDL.Controls.Add(cbxMain);
24 pnl_DDL.Controls.Add(cbxSub);
25 }
26
27 protected void cbxSub_Callback( object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
28 {
29 ASPxComboBox cbxSub = source as ASPxComboBox;
30 ASPxComboBox cbxMain = this .pnl_DDL.FindControl( " cbx_Main " ) as ASPxComboBox;
31 string value = "" ;
32 if (cbxMain.SelectedItem != null )
33 value = cbxMain.SelectedItem.Value.ToString();
34 string subSQL = " select * from sub_table where eat=' " + value + " ' " ;
35 cbxSub.DataSource = SQLHELPER.GetDataSet(subSQL);
36
37 cbxSub.TextField = " E " ;
38
39 cbxSub.ValueField = " eaf " ;
40 cbxSub.DataBind();
41 }
2 {
3 ASPxComboBox cbxMain = new ASPxComboBox();
4 cbxMain.ID = " cbx_Main " ;
5 cbxMain.DataSource = SQLHELPER.GetDataSet( " select eat, name from main_table " );
6 cbxMain.ClientInstanceName = " cbx_Main " ;
7 cbxMain.TextField = " name " ;
8 cbxMain.ValueField = " eat " ;
9 cbxMain.DataBind();
10 cbxMain.ClientSideEvents.SelectedIndexChanged = " function(s, e) { OnCountryChanged(s); } " ;
11
12 ASPxComboBox cbxSub = new ASPxComboBox();
13 cbxSub.ID = " cbx_Sub " ;
14 cbxSub.ClientInstanceName = " cbx_Sub " ;
15 cbxSub.Callback += new DevExpress.Web.ASPxClasses.CallbackEventHandlerBase(cbxSub_Callback);
16
17 // 注册脚本
18 string js = " <script language=\ " javascript\ " >function OnCountryChanged(cbx_Main) { " ;
19 js += " cbx_Sub.PerformCallback(cbx_Main.GetValue().toString()); " ;
20 js += " }</script> " ;
21
22 ClientScript.RegisterClientScriptBlock( this .GetType(), " OnCountryChanged " , js);
23 pnl_DDL.Controls.Add(cbxMain);
24 pnl_DDL.Controls.Add(cbxSub);
25 }
26
27 protected void cbxSub_Callback( object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
28 {
29 ASPxComboBox cbxSub = source as ASPxComboBox;
30 ASPxComboBox cbxMain = this .pnl_DDL.FindControl( " cbx_Main " ) as ASPxComboBox;
31 string value = "" ;
32 if (cbxMain.SelectedItem != null )
33 value = cbxMain.SelectedItem.Value.ToString();
34 string subSQL = " select * from sub_table where eat=' " + value + " ' " ;
35 cbxSub.DataSource = SQLHELPER.GetDataSet(subSQL);
36
37 cbxSub.TextField = " E " ;
38
39 cbxSub.ValueField = " eaf " ;
40 cbxSub.DataBind();
41 }