C# ASP Gridview下拉列表选择已更改,如何填充除rowDataBound()之外的下拉列表
我在ASP中有一个C# ASP Gridview下拉列表选择已更改,如何填充除rowDataBound()之外的下拉列表,c#,asp.net,C#,Asp.net,我在ASP中有一个gridview,有模板字段包含选择,编辑和删除链接每一行,以及包含插入链接的页脚 每行有两个下拉列表,比如:类别和子类别,当我更改类别下拉列表的内容时,子类别下拉列表应自动显示相应的内容 我试图在SelectedIndexChanged上编写处理程序,但不知道如何继续有什么想法吗?(请记住,我编写了所有rowDataBound()代码来填充下拉列表) 换句话说,如何填充除行中的数据绑定()之外的dropdownlist 代码: 在这里,我找到了来自GridViewRowEve
gridview
,有模板字段
包含选择
,编辑
和删除
链接每一行,以及包含插入
链接的页脚
每行有两个下拉列表
,比如:类别
和子类别
,当我更改类别下拉列表
的内容时,子类别下拉列表
应自动显示相应的内容
我试图在SelectedIndexChanged上编写处理程序,但不知道如何继续有什么想法吗?(请记住,我编写了所有rowDataBound()代码来填充下拉列表)
换句话说,如何填充除行中的数据绑定()之外的dropdownlist
代码:
在这里,我找到了来自GridViewRowEventArgs
在selectedIndexChanged处理程序中,如何找到DropDownList?
DropDownList ddlcontegory=(DropDownList)e.Row.FindControl(“ddlcontegory”)
不起作用FindControl不是递归的,在到达控件所在的级别之前,gridview行的Cells
属性中有许多TableCell
控件,因此,将其更改为类似以下内容将起作用:
TableCell cell = (TableCell)e.Row.FindControl("idOfMyCellIfItHasOne");
DropDownList ddlCategory = (DropDownList)cell.FindControl("ddlCategory");
或者,如果您的单元格/列没有ID和/或您知道该单元格在表中的位置不会改变,则可以使用Cells
属性上的索引器:
DropDownList ddlCategory = (DropDownList)e.Row.Cells[cellIndex].FindControl("ddlCategory");
听起来您想使用在类别dropdownlist中选择的值将数据绑定到子类别dropdownlist。你可以做:
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)((DropDownList)sender).Parent.Parent;
DropDownList ddlSubCategory = (DropDownList)row.FindControl("ddlSubCategory");
ddlSubCategory.DataSource = //whatever you want to bind, e.g. based on the selected value, using((DropDownList)sender).SelectedValue;
ddlSubCategory.DataBind();
}
如果我误解了您的意思,请在评论中更正。在您的下拉列表中使用AutoPostBack=“true”
就像这样:
<asp:DropDownList ID="footer_id" AutoPostBack="true"
OnSelectedIndexChanged="footer_id_SelectedIndexChanged"
runat="server"></asp:DropDownList>
是否在DropDownList中添加了onSelectedIndexChanged事件?或者你只是在c#中创建了它?我添加了它,它被编译器读取,但是我被卡住了,我不知道如何继续,为什么被卡住了?在该函数中,绑定另一个dropdownlist;获取所需的元素并进行绑定();哦,我明白了,它在一排!那么您在哪里创建了事件处理程序??这里有一些类似的东西,希望能有所帮助这是我一直在寻找的答案,非常感谢@AGuyCalledGerald
<asp:DropDownList ID="footer_id" AutoPostBack="true"
OnSelectedIndexChanged="footer_id_SelectedIndexChanged"
runat="server"></asp:DropDownList>