2012-05-01 110 views
0

我有一個下拉列表列的gridview,我啓用了分頁功能。問題在於它每次轉到下一頁後,上一頁上的下拉列表的選定值將恢復爲默認值。閱讀從gridview下拉列表的值

我試着用if(!ispostback)包住代碼,只在第一頁提供的其他頁面消失

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      List<CPDEmployee> employeelist = (List<CPDEmployee>)Cache["EmployeeList"]; 

      unverifiedlist.DataSource = employeelist; 
      unverifiedlist.AllowPaging = true; 
      unverifiedlist.PageSize = 10; 
      unverifiedlist.DataBind(); 
     } 
    } 
protected void PageSelect_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int page = int.Parse(PageSelect.SelectedItem.Text); 
    unverifiedlist.PageIndex = page; 
    DataBind(); 
} 





<asp:GridView ID="unverifiedlist" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" ViewStateMode="Enabled"> 
         <Columns><asp:TemplateField HeaderText="Options" > 
           <ItemTemplate> 
            <asp:DropDownList ID="options" runat="server" AutoPostBack="true"> 
             <asp:ListItem Value="1">Verified</asp:ListItem> 
             <asp:ListItem Value="0">Rejected</asp:ListItem> 
            </asp:DropDownList> 
           </ItemTemplate> 
          </asp:TemplateField> 
        </Columns> 
        <PagerSettings Visible="false"/>    
     </asp:GridView> 
<asp:DropDownList ID="PageSelect" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageSelect_SelectedIndexChanged"></asp:DropDownList> 

沒有人知道如何解決它,我應該在哪裏放的IsPostBack?謝謝

+0

我希望這個鏈接將幫助您解決問題。 http://stackoverflow.com/questions/4189158/asp-net-dropdownlist-not-retaining-selected-item-on-postback –

+0

我增加了更多的代碼。我在girdview中的下拉列表沒有數據源,只有兩個值。還有另一個下拉列表來控制gridview分頁。當它轉到下一頁時,頁面發回,上一頁的下拉列表恢復爲默認值。 – pita

回答

1

您需要處理OnRowDataBound並以編程方式設置適當的元素。例如:

<asp:GridView ID="unverifiedlist" runat="server" 
    OnRowDataBound="unverifiedlist_RowDataBound" AutoGenerateColumns="false" 
    AllowSorting="true" AllowPaging="true" ViewStateMode="Enabled"> 

並實行類似:

protected void unverifiedlist_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
    ((DropDownList)e.Row.FindControl("options")).SelectedValue=((CPDEmployee)e.Row.DataItem).Unverified; 

    } 
} 

假設,很顯然,你有一個屬性叫做未驗證您的業務對象。你應該使用適當的東西。這只是一個例子。

UPDATE:

由於下拉網格內是自動回發,我想補充的OnSelectedIndexChanged事件處理程序下拉網格內的列表。喜歡的東西:

<asp:DropDownList ID="options" runat="server" AutoPostBack="true" OnSelectedIndexChanged="options_SelectedIndexChanged"> 
    <asp:ListItem Value="1">Verified</asp:ListItem> 
    <asp:ListItem Value="0">Rejected</asp:ListItem> 
</asp:DropDownList> 

然後

protected void options_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string selecteValue = ((DropDownList)sender).SelectedValue; 
    //Now persist this value in the appropriate business object 
    //this is the difficult part because you don't know for which row in the gridview 
    //you are changing this selection. You'll need to devise a way to pass an extra 
    //value (an Employee ID, I would imagine) that would allow you to grab the 
    // record from the List<CDPEmployee> and change the property to the new selection. 
} 
+0

@lcarus非常感謝,這是我需要的,但我只是有一個問題,我想下拉列表保持我選擇的價值,而不是預填充價值所以,你可以告訴我如何保存選擇的價值。謝謝 – pita

+0

您需要將選擇基本持久化到綁定到網格的業務對象。我可以想到很多這樣做的方法,但它們都是黑客,因爲GridView中的下拉列表需要能夠告訴你哪個CDPEmployee你正在改變選擇,所以你可以從緩存中獲取列表 - 這是我看到的地方您存儲CDPEmployee的列表並將相應的屬性更新爲適當的對象。我會發佈一個起點的方法。 – Icarus