如何从Gridview中的其他下拉列表的值绑定下拉列表

时间:2015-09-03 08:41:53

标签: c# asp.net

我确实有一个gridview,里面有3个下拉列表。 例如:如果网格视图有15行,则每行有3个下拉列表

1)使用第一个下拉列表的选定索引更改第一个下拉列表绑定第二个下拉列表

2)使用第二个下拉列表的选定索引更改的第二次下拉值绑定第三次下拉列表

那我怎么能实现呢?

1 个答案:

答案 0 :(得分:0)

在级联下拉列表中将AutoPostBack设置为true。处理下拉列表中的SelectedIndexChanged事件。将GridView本身数据绑定不在回发上非常重要,因此请使用IsPostBack属性。否则事件不会被触发。

protected void Ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl1 = (DropDownList)sender;
    GridViewRow row = (GridViewRow)ddl1.NamingContainer;
    DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2ID");
    // here get the datasource for ddl2 according to ddl1.SelectedValue
    ddl2.DataSource = GetDataSource(ddl1.SelectedValue);
    ddl2.DataBind();
}

protected void Ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl2 = (DropDownList)sender;
    GridViewRow row = (GridViewRow)ddl2.NamingContainer;
    DropDownList ddl3 = (DropDownList)row.FindControl("DropDownList3ID");
    // here get the datasource for ddl3 according to ddl1.SelectedValue
    ddl3.DataSource = GetDataSource(ddl2.SelectedValue);
    ddl3.DataBind();
}
相关问题