C#下实现主从DropDownList互动的方法

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
相信和我一样,有很多同行都遇到主从dropdownlist互动的问题,比如选择了县,那么让系统自动在dropdownlist2中列出该县下属的乡名列表,而选了乡后,再在dropdownlist3中列出该乡下属的村的列表,那么我以前的解决方法是重新Rill相应dropdownlist所绑定的dataset,这样费事费资源,而且麻烦,其实我们可以用RowFilter来实现,下面是我的具体实现方法

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
InitA();
}

//初始化dorpdownlist

DataSet Myds;
//CConection 为我的自定义类;实现与数据库的连接,其中有一属性为cnn,为OleDbConnection.
CConection Mycnn;

public void InitA()
{
Mycnn=new CConection();
string strSql;
strSql="select 编号,名称 from sys_county order by 编号";
OleDbDataAdapter MyoleAp=new OleDbDataAdapter(strSql,Mycnn.Cnn) ;
Myds=new DataSet() ;
MyoleAp.Fill(Myds,"sys_county");
this.DropDownList1.DataSource=Myds.Tables["sys_county"];
this.DropDownList1.DataValueField="编号";
this.DropDownList1.DataTextField="名称";
this.DropDownList1.DataBind();
strSql="select 编号,名称,所属县 from sys_town order by 编号";
MyoleAp.SelectCommand.CommandText=strSql;
MyoleAp.Fill(Myds,"sys_town");
this.DropDownList2.DataSource=Myds.Tables["sys_town"];
this.DropDownList2.DataValueField="编号";
this.DropDownList2.DataTextField="名称";
MyoleAp.Dispose();
}

//DropDownList1的changed改变dorpdownlist2的显示值,

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Myds.Tables["sys_town"].DefaultView.RowFilter="所属县='" + this.DropDownList1.SelectedValue +"'";
this.DropDownList2.DataBind();
}
[注意:]以上代码在C#的webform下实现,dropdownlist1的autopostback必须为true.

11AJ2c2310Xb2.jpg
seover="window.status='正文--C#下实现主从DropDownList互动方法';return true">
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
上一篇从C/C++迁移到PHP&#8212;&#8212;判断字符类型的函数
下一篇在线增减.htpasswd内的用户
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭