访问gridview中的下拉列表(选定值)

时间:2011-11-06 22:36:10

标签: asp.net gridview drop-down-menu

我在aspx格式视图和下拉列表中获取下拉列表选择值时遇到了一些麻烦:

<asp:GridView ID="grid1" runat="server" autogeneratecolumns="true" >
    <Columns>
        <asp:BoundField HeaderText="something" />
        <asp:TemplateField HeaderText="filtras">
            <HeaderTemplate>
                <asp:DropDownList ID="dropdown1" runat="server"
                    OnLoad="dropdownLoad"
                    OnSelectedIndexChanged="updatetable" />
            </HeaderTemplate>
         </asp:TemplateField>
    </Columns>
</asp:GridView>

我们使用DropDownList事件向OnLoad填充值,然后当我们从DropDownList中选择某些内容时,事件OnSelectedIndexChange应该允许我们获取所选值并用它做我们想做的事(在这种情况下过滤网格),但OnSelectedIndexChange永远不会被执行。

protected void Page_Load(object sender, EventArgs e)
{ 
    //Create Gridview + fill with values
    if (IsPostBack)
    {
        return;
    }

    ArrayList mycountries = new ArrayList();
    mycountries.Add("Norway");
    mycountries.Add("Sweden");
    mycountries.Add("France");
    mycountries.Add("Italy");
    mycountries.TrimToSize();
    mycountries.Sort();

    rb.DataSource = mycountries;
    rb.DataBind();

    grid1.DataSource = mycountries;
    grid1.DataBind();
}

protected void dropdownLoad(object sender, EventArgs e)
{   // fill dropDownList in GridView with data
    DropDownList dropdown = sender as DropDownList;
    if (dropdown != null)
    {
        ArrayList mycountries = new ArrayList();
        mycountries.Add("Norway");
        mycountries.Add("Sweden");
        mycountries.Add("France");
        mycountries.Add("Italy");
        mycountries.TrimToSize();
        mycountries.Sort();

        dropdown.DataSource = mycountries;
        dropdown.DataBind();

        TextBox1.Text = dropdown.SelectedIndex.ToString();
    }
}

protected void updatetable(object sender, EventArgs e)
{// after dropDownList element was selected change dropdownlist values/or filter the table...
 //this part is never executed ! Why?

    DropDownList dropdown = sender as DropDownList;
    if (dropdown != null)
    {
        ArrayList mycountries = new ArrayList();
        mycountries.Add("UK");
        mycountries.Add("USA");
        mycountries.Add("Sweden");
        mycountries.Add("Hungary");
        mycountries.TrimToSize();
        mycountries.Sort();

        dropdown.DataSource = mycountries;
        dropdown.DataBind();
    }
}

如何获得DropDownList的选定值?我的调试器显示永远不会执行OnSelectedIndexChange。 我尝试按照此问题Setting selectedvalue for a dropdownlist in GridView中的建议进行操作,但这不起作用。

1 个答案:

答案 0 :(得分:0)

您是否尝试将下拉控件的AutoPostBack设置为true,以便导致回发?