2011-11-06 139 views
1

我有越来越下拉列表中选择值有点麻烦,我们在ASPX一个GridView,并在其下拉列表:Acessing下拉列表(选择值)的GridView

<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提出的建议,但没有奏效。

回答

0

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

+0

你是我的英雄! thx你是一个回报礼物,我会给你一个很酷的图片;)http://www.cha.lt/uploads/posts/2011-11/1320474024_15.jpg http://www.cha.lt/category/ paveiksliukai/66617-o-koks-jusu-megstamiausias-pesonazas.html – Greeed

+0

很高兴为您提供帮助。爱第一个形象,完美的随机性! –