2011-11-06 138 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

很高興爲您提供幫助。愛第一個形象,完美的隨機性! –