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

解决方法:

第一步:在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();
            }
        }

 

posted @ 2011-05-23 12:05  瑞君  Views(772)  Comments(1Edit  收藏  举报