工作小结(二十三)-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();
}
}
第一步:在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();
}
}
相关文章推荐
- Gridview与DropDownList联动添加数据实时更新
- Gridview与DropDownList联动添加数据实时更新
- GridView中DropdownList,Textbox同一行无刷新联动
- gridview中连个dropdownlist联动
- 笔记:不写一行代码,实现 DropDownList 和 GridView 联动
- 工作小结(十八)-Gridview使用二[在不同的地方查找Textbox和主键]
- 工作小结(十三)-GridView的使用
- GridView中实现DropDownList联动
- GridView中多个DropDownList的联动问题
- GridView编辑状态下DropDownList的联动
- GridView编辑状态下DropDownList的联动
- GridView中实现DropDownList联动
- GridView中DropDownList联动
- GridView中两个DropDownList联动
- 工作小结(十七)-GridView 翻页后保存CheckBox列的选中状态
- gridview中连个dropdownlist联动
- 图片式教程:不写一行代码,实现 DropDownList 和 GridView 联动
- 2013年2月工作小结 -- 最美工作月
- asp.net实现DropDownList、ListBox无刷新三级联动的方法
- TSH(Tapestry Spring Hibernate)整合工作小结(转)