gridview中连个dropdownlist联动

gridview中连个dropdownlist联动
2008-08-18 09:47
asp.net中GridView实现DropDownList级联动

前几天写过文章,介绍了如何在DetailsView控件里实现DropDownList的联动,有人问我如何在GridView中来实现呢,今天我简单介绍办法:


首先还是为GridView控件增加两个模板列用来存放实现联动的DropDownList:

<asp:TemplateField HeaderText="乡">

<EditItemTemplate><asp:HiddenField ID="HDXiang" runat="server" Value='<{5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}#Bind("地点_乡") {5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}>' /><asp:DropDownList ID="DDXiangz" runat="server" Width="90px" AutoPostBack="true" OnSelectedIndexChanged="ddlXiang_SelectedIndexChanged2"/></EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label12" runat="server" Text='<{5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}#Eval("地点_乡") {5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

   <asp:TemplateField HeaderText="村">

     <EditItemTemplate><asp:HiddenField ID="HDCun" runat="server" Value='<{5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}#Bind("地点_村") {5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}>' /><asp:DropDownList ID="DDlCunz" runat="server" Width="90px" /></EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label24" runat="server" Text='<{5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}# Eval("地点_村"){5351c8a62f918c9a2549d9888258dfab50bc32b8c1160430abfd6b919b9dbb4d}>'></asp:Label>

    </ItemTemplate>

</asp:TemplateField>

小解:

我这里做的是乡和村的联动。在两个模板列的编辑模板下分别增加了HiddenField 控件,这里用来记录当前数据库中存放的乡和村的值记录,当用户单击编辑时通过它们来设置DropDownList 的默认选项。

乡的OnSelectedIndexChanged事件是来处理乡变化时村也随之联动,注,AutoPostBack需要设置为true.

OK, 再看下.cs文件里实现,这里主要涉及到两个事件OnSelectedIndexChanged事件和RowDataBound事件,一个一个看吧:

RowDataBound:
```c#
protected void GVData_RowDataBound(object sender, GridViewRowEventArgs e)

<pre class="prism-highlight line-numbers" data-start="1"><code class="language-null">{

commonData mycd = new commonData();

if (((DropDownList)e.Row.FindControl("DDLxz")) != null)

{

//绑定乡

DropDownList DDXiang = (DropDownList)e.Row.FindControl("DDXiangz");

DDXiang.DataSource =您的乡数据源,从数据库读取;

DDXiang.DataTextField = "乡名";

DDXiang.DataValueField = "编号";

DDXiang.DataBind();

// 通过HiddenField选中现在的 DropDownList的值

DDXiang.SelectedValue = ((HiddenField)e.Row.FindControl("HDXiang")).Value;

//绑定村

DropDownList DDCun = (DropDownList)e.Row.FindControl("DDlCunz");

DDCun.DataSource = 您的村数据源,从数据库读取;

DDCun.DataTextField = "村名";

DDCun.DataValueField = "编号";

DDCun.DataBind();

// 通过HiddenField选中现在的 DropDownList的值

DDCun.SelectedValue = ((HiddenField)e.Row.FindControl("HDCun")).Value;

}
</code></pre>

<pre class="line-numbers prism-highlight" data-start="1"><code class="language-null"><br />小解:

这个是针对我的GridView控件的RowDataBound事件来说的,当行数据绑定时进行动态绑定乡和村和DropDownList(从数据库中读取数据)。

OK,实现了乡和村的数据绑定后,我们看下它们的级联动问题(当单击编辑时,乡改变时动态获取与之对应的村的数据)

OnSelectedIndexChanged:
```c#
/// <summary>

/// 编辑时村随着乡变化

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

public void ddlXiang_SelectedIndexChanged2(object sender, EventArgs e)

{

DropDownList DDXiang= (DropDownList)sender;//获取现在的事件触发者:乡的DropDownList

GridViewRow gvr = (GridViewRow)DDXiang.NamingContainer;//乡与村同属于在一个NamingContainer下

DropDownList DDCun = (DropDownList)gvr.FindControl("DDlCunz");//找到村的DropDownList

DDCun.DataSource = getCommon_cun(DDXiang.SelectedValue).Tables[0].DefaultView;//这个函数是我自定义的,通过乡的编辑取得与之对应的村的数据集

DDCun.DataTextField = "村名";

DDCun.DataValueField = "编号";

DDCun.DataBind();

}

小解:

上述关键是找到当前要操作的乡的DropDownList和村的DropDownList,乡的DropDownList好找,可以通过sender获取,与之对应的村的DropDownList因为与乡的DropDownList同属于在一个NamingContainer下,所以也可以找到它,这样就可以通过乡的DropDownList编号对村的DropDownList实现数据绑定了。

关于NamingContainer我也写过文章介绍,详情请查看《asp.net中的NamingContainer详解》

说点什么

avatar
  Subscribe  
提醒