2

gridview在 ASP 中有一个,template Fields包含和链接每一行,以及包含链接的页脚。selecteditdeleteinsert

dropdownlists每行有两个,比方说:Categorysub-category,当我更改内容时category DropDownListsub-category DropDownList应该自动显示相应的内容。

我试图编写一个onSelectedIndexChanged处理程序,但我不知道如何继续。有任何想法吗? (请记住,我完成了所有的 rowDataBound() 代码来填充下拉列表)

换句话说,如何填充除 row_databound() 之外的下拉列表

代码:

protected void grdBulkScheduler_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {   
                DropDownList ddlCategory = (DropDownList)e.Row.FindControl("ddlCategory");
                if (ddlCategory != null)
                {
                    ddlCategory .DataSource = cData.GetCategory();
                    ddlCategory .DataValueField = "c_ID";
                    ddlCategory .DataTextField = "c_Text";
                    ddlCategory .DataBind();                    
                }

在这里,我从GridViewRowEventArgs中找到下拉列表类别

在 selectedIndexChanged 处理程序中,如何找到 DropDownList? 因为 DropDownList ddlCategory = (DropDownList)e.Row.FindControl("ddlCategory")不工作

4

3 回答 3

2

听起来您想使用您在类别下拉列表中选择的值将数据绑定到子类别下拉列表。你可以做:

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();
}

如果我误解了你,请在评论中纠正我。

于 2013-05-24T15:31:23.163 回答
1

FindControl 不是递归的,并且在您到达控件所在的级别之前,您的属性中有许多TableCell控件,因此将其更改为类似这样的内容将起作用:CellsGridViewRow

TableCell cell = (TableCell)e.Row.FindControl("idOfMyCellIfItHasOne");
DropDownList ddlCategory = (DropDownList)cell.FindControl("ddlCategory");

或者,如果您的单元格/列没有 ID 和/或您知道表格中单元格的位置不会改变,您可以在Cells属性上使用索引器:

DropDownList ddlCategory = (DropDownList)e.Row.Cells[cellIndex].FindControl("ddlCategory");
于 2013-05-24T15:10:47.167 回答
0

在您的下拉菜单中使用 AutoPostBack="true"

像那样:

 <asp:DropDownList ID="footer_id"  AutoPostBack="true" 
 OnSelectedIndexChanged="footer_id_SelectedIndexChanged" 
 runat="server"></asp:DropDownList>
于 2017-05-15T13:19:59.390 回答