1

好的,所以我知道标题不是很好,但我会尝试解释。

我有一个网格视图,其中有一个下拉菜单作为模板字段。我想拥有它,以便如果有人在下拉列表中选择某些内容,gridview 中的所有下拉列表都会更改为该值。这就是我的想法,但请参阅评论:

protected void dcDdl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (recPartDdl.SelectedItem.Text != "All")
        {
           string value = //Need to get the value of the changed drop down.

            foreach(GridViewRow row in allRmaGv.Rows)
            {
                DropDownList dcDropDown = (DropDownList)row.FindControl("dcDdl");
                dcDropDown.SelectedValue = value;

            }
        }
    }

我需要为网格视图中的类似过滤器的 excel 执行此操作。

4

2 回答 2

0

在遍历 GridView 中的行时,您需要过滤掉行类型,这样您就不会尝试在页眉行和/或页脚行中查找控件,因此请在foreeach循环中尝试以下操作:

foreach(GridViewRow row in allRmaGv.Rows)
{
    // Try to find the `dcDdl` in data rows only, because they do not exist in header or footer rows
    if (row.RowType == DataControlRowType.DataRow)
    {
        DropDownList dcDropDown = (DropDownList)row.FindControl("dcDdl");
        dcDropDown.SelectedValue = value;
    }
}

更新:

要获取所选下拉列表的值,请执行以下操作:

protected void dcDdl_SelectedIndexChanged(object sender, EventArgs e)
{
    if (recPartDdl.SelectedItem.Text != "All")
    {
        string value = (sender as DropDownList).SelectedValue.ToString();

        foreach(...)
        {
            ...
        }
    }
}
于 2013-08-15T21:08:26.507 回答
0

试试这个:

    protected void dcDdl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (recPartDdl.SelectedItem.Text != "All")
        {
            DropDownList ddl = (DropDownList)sender;

            //First check type of ddl.SelectedValue and then process
            string value = ddl.SelectedValue;

            foreach(GridViewRow row in allRmaGv.Rows)
            {
                DropDownList dcDropDown = (DropDownList)row.FindControl("dcDdl");
                dcDropDown.SelectedValue = value;
            }
        }
    }
于 2013-08-16T09:08:35.493 回答