您的位置:首页 > 运维架构

工作小结(二十三)-gridview中dropdownlist联动

2011-05-23 12:05 417 查看
解决方法:

第一步:在GridView模板列中添加DropDownList并设置AutoPostback属性为true。

第二步:再在DropDownList的SelectedIndexChanged事件里写下面几句话。

  DropDownList ddl = (DropDownList)sender; //得到当前的DropDownList

  GridViewRow gvr = (GridViewRow)ddl.NamingContainer; //获取对DropDownList的容器引用

第三步://得到gvr后就好办了查找下面的子控件。

  DropDownList ddl2 = (DropDownList)gvr.FindControl("DDl2")

  再给ddl2绑定数据

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

<asp:TemplateField HeaderText="乡">

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

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="村">

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

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

后台代码如下:

protected void ddlParentTile_SelectedIndexChanged(object sender, EventArgs e)
{
Business.Module.Inc.BLL.tblIncConstantDef tblIncConstantDef = new Business.Module.Inc.BLL.tblIncConstantDef();

DropDownList ddl = (DropDownList)sender; //得到当前的DropDownList
GridViewRow gvr = (GridViewRow)ddl.NamingContainer; //获取对DropDownList的容器引用
DropDownList ddlChildTitle = (DropDownList)gvr.FindControl("ddlChildTitle");

if (!string.IsNullOrEmpty(ddl.SelectedValue))
{
ddlChildTitle.DataSource = tblIncConstantDef.GettblIncConstantDefByParentID(ddl.SelectedValue);
ddlChildTitle.DataValueField = "ID";
ddlChildTitle.DataTextField = "Item";
ddlChildTitle.DataBind();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: