2011-06-28 30 views
1

我有一個4列的網格視圖,包括一列作爲批准。批准是有一個複選框,將綁定後重複數據集的所有行。我已將allowpaging = true & pageindexsize設置爲10.現在,假設我選中第2行和第5行的複選框,然後移動到第2頁,然後回到第1頁,我檢查過的複選框(第2行和第5行)第1頁正在重置爲未檢查狀態。我知道原因,這是由於我們在onpageindexchanging事件上進行的gridview綁定。但是,即使我們從1頁移動到另一頁,是否還有辦法維持複選框的狀態。如何在啓用了分頁的GridView中維護控件的狀態?

謝謝

回答

1

您可以使用Session來維護這些值。一個頁面索引更改事件需要將值添加到會話並重新綁定複選框。

下面是幾個環節可能幫助你

  1. http://forums.asp.net/p/1368550/2852029.aspx
  2. http://myaspsnippets.blogspot.com/2010/08/maintaining-state-of-checkboxes-while.html
+0

我應該在會話中存儲什麼?複選框被選中的gridview的行索引? – NayeemKhan

+0

嘗試我提供的第二個鏈接,它有一個示例代碼,它可以幫助你。 – Anuraj

+1

謝謝。第二個鏈接給出了我的答案,任何在搜索中的人都可以看起來一樣。花了點時間,因爲我沒有時間回到這裏,並將其標記爲答案,爲此。 – NayeemKhan

0

嘗試,(你必須設置兩個事件Checkbox_Checked和Checkbox_PreRender在CheckBox控件並且必須在網格中設置DataKey,以便在Checks數組中有一個索引。)

protected bool[] Checks 
{ 
    get { return (bool[])(ViewState["Checks"] ?? new bool[totalLengthOfDataSource]); } 
    set { ViewState["Checks"] = value; } 
} 

protected void Checkbox_Checked(object sender, EventArgs e) 
{ 
    CheckBox cb = (CheckBox)sender; 
    bool[] checks = Checks; 
    checks[(int)GetRowDataKeyValue(sender)] = cb.Checked; 
    Checks = checks; 
} 

protected void Checkbox_PreRender(object sender, EventArgs e) 
{ 
    CheckBox cb = (CheckBox)sender; 
    bool[] checks = Checks; 
    cb.Checked = checks[(int)GetRowDataKeyValue(sender)]; 
} 

這些工作最好在靜態GridViewUtils類中。

public static GridViewRow GetRow(object sender) 
{ 
    Control c = (Control)sender; 
    while ((null != c) && !(c is GridViewRow)) 
     c = c.NamingContainer; 
    return (GridViewRow)c; 
} 

/// <summary> 
/// Get the Grid the row is in 
/// </summary> 
/// <param name="row"></param> 
/// <returns></returns> 
public static GridView GetGrid(this GridViewRow row) 
{ 
    Control c = (Control)row; 
    while ((null != c) && !(c is GridView)) 
     c = c.NamingContainer; 
    return (GridView)c; 
} 

/// <summary> 
/// Get the ID field value based on DataKey and row's index 
/// </summary> 
/// <param name="sender">Any web-control object in the grid view</param> 
/// <returns></returns> 
public static object GetRowDataKeyValue(object sender) 
{ 
    try 
    { 
     GridViewRow row = GetRow(sender); 
     GridView grid = row.GetGrid(); 
     return grid.DataKeys[row.RowIndex].Value; 
    } 
    catch 
    { 
     return null; 
    } 
} 
0

另一種方法:

創建(或類似)被稱爲「使用者所選擇的」你可綁定對象上的布爾屬性 您可綁定列表對象 上創建的對象ID索引器在grid_RowDataBound添加綁定對象的ID作爲複選框的一個屬性 對grid_RowDataBound也設置您的複選框的選中屬性爲obj.userSelected 在chech_CheckChaged上使用您的索引器設置您的綁定對象的userSelected屬性

感謝

+0

請格式化您的答案,使用輸入和'''符號來強調例如屬性名稱 –

0

我試圖預渲染技術從@Chuck,但它並沒有給我(VS2005,ASP.net2,SQLsrv2005)工作。也許是過時的技術,但這是客戶所擁有的。

因此,我嘗試了簡單的myaspsnippets技術,並進行了一些修改,它完美的工作!

我的GridView

   <asp:GridView ID="gvFTUNSENT" runat="server" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="False" CssClass="gvCSS" Width="100%" 
        DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT" 
        GridLines="None" AllowPaging="True" PageSize="10" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
        OnPageIndexChanged="GridView_PageIndexChanged" 
        OnPageIndexChanging="GridView_PageIndexChanging"> 
        <RowStyle Wrap="True" Height="48px" /> 
        <Columns> 
...etc... 
        </Columns> 
        <FooterStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" Height="100%" /> 
        <PagerStyle CssClass="cssPager" BackColor="#6B696B" ForeColor="White" HorizontalAlign="Left" Height="100%" /> 
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
       </asp:GridView> 

的Pageing我使用的方法是:

protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView gv = (GridView)sender; 

    //saves a "copy" of the page before it's changed over to the next one 
    SavePageState(gv); 

    gv.PageIndex = e.NewPageIndex; 
    gv.DataBind(); 
} 

protected void GridView_PageIndexChanged(object sender, EventArgs e) 
    { 
...your code to handle anything after the page has changed 

        gv.DataSource = dt; 
        gv.DataSourceID = null; 
        gv.DataBind(); 

        //reload any checkboxes that were session saved in the page 
        LoadPageState(gv); 
       } 
      } 
     } 
    } 

所以是保存和加載方法如下:

private void SavePageState(GridView gv) 
{ 
    ArrayList categoryIDList = new ArrayList(); 
    Int32 index = -1; 
    foreach (GridViewRow row in gv.Rows) 
    { 
     HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID"); 
     if (hfStudentUnitID != null) 
     { 
      if (hfStudentUnitID.Value.Length > 0) 
      { 
       index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"]; 
       bool result = ((CheckBox)row.FindControl("cbSEND")).Checked; 

       // Check in the Session 
       if (Session["CHECKED_ITEMS"] != null) 
        categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; 
       if (result) 
       { 
        if (!categoryIDList.Contains(index)) 
         categoryIDList.Add(index); 
       } 
       else 
        categoryIDList.Remove(index); 
      } 
     } 
    } 
    if (categoryIDList != null && categoryIDList.Count > 0) 
     Session["CHECKED_ITEMS"] = categoryIDList; 
} 

private void LoadPageState(GridView gv) 
{ 
    ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; 
    if (categoryIDList != null && categoryIDList.Count > 0) 
    { 
     foreach (GridViewRow row in gv.Rows) 
     { 
      HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID"); 
      if (hfStudentUnitID != null) 
      { 
       if (hfStudentUnitID.Value.Length > 0) 
       { 
        Int32 index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"]; 
        if (categoryIDList.Contains(index)) 
        { 
         CheckBox myCheckBox = (CheckBox)row.FindControl("cbSEND"); 
         myCheckBox.Checked = true; 
        } 
       } 
      } 
     } 
    } 
} 

爲了使這項工作對你來說,你需要把尋呼方法調用到你的GridView,改變從cbSEND將CheckBox ID到你所使用的,與以往點HiddenFields到某些其他控件或值爲您的行具有唯一標識符。 不要使用RowIndex,因爲這在GridView中的數據長度上並不是唯一的。

工程就像一個魅力!