2011-03-14 27 views
0

我想在網格視圖中顯示的項目選擇上保留複選框值。在分頁的GridView中持久化複選框值asp.net/c#

這是gridviewpage.aspx它創建GridView控件的代碼:

<form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="GridView1" runat="server" 
     AutoGenerateColumns = "False" Font-Names = "Arial" 
     Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
     HeaderStyle-BackColor = "green" AllowPaging ="True" 
     OnPageIndexChanging = "OnPaging1xxx" OnRowDataBound = "RowDataBound" 
      AllowSorting="True" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" > 
     <Columns> 
     <asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID" 
       HeaderText = "CustomerID" ReadOnly="True" SortExpression="CustomerID" /> 
     <asp:BoundField ItemStyle-Width = "150px" DataField = "City" HeaderText = "City" 
       SortExpression="City"/> 
     <asp:BoundField ItemStyle-Width = "150px" DataField = "Country" 
       HeaderText = "Country" SortExpression="Country"/> 
     <asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode" 
       HeaderText = "PostalCode" SortExpression="PostalCode"/> 
       <asp:TemplateField> 
       <ItemTemplate> 
       <asp:CheckBox id="CheckBox1" runat="server" onclick="Check_Click(this)"/> 
       </ItemTemplate> 
       <HeaderTemplate> 
       <asp:CheckBox id="chkAll" runat="server" Text="Select All" onclick="checkAll(this)"/> 
       </HeaderTemplate> 
       </asp:TemplateField> 
     </Columns> 
     <AlternatingRowStyle BackColor="#C2D69B" /> 

<HeaderStyle BackColor="Green"></HeaderStyle> 
    </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="Data Source=SDANTURTHI-PC;Initial Catalog=ArticleDB;Integrated Security=True" 
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT [CustomerID], [City], [Country], [PostalCode] FROM [Customers]"> 
     </asp:SqlDataSource> 
    </div> 

這是gridviewpage.aspx.cs

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
using System.Collections; 
using System.IO.Compression; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    {   
     ArrayList CheckBoxArray; 
     if (ViewState["CheckBoxArray"] != null) 
     { 
      CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"]; 
     } 
     else 
     { 
      CheckBoxArray = new ArrayList(); 
     } 

     if (IsPostBack) 
     { 
      int CheckBoxIndex; 
      bool CheckAllWasChecked=false; 
      CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[4].FindControl("chkAll"); 
      string checkAllIndex = "chkAll-" + GridView1.PageIndex; 
      if (chkAll.Checked) 
      {     
       if (CheckBoxArray.IndexOf(checkAllIndex) == -1) 
       { 
        CheckBoxArray.Add(checkAllIndex); 
       } 
      } 
      else 
      { 
       if (CheckBoxArray.IndexOf(checkAllIndex) != -1) 
       { 
        CheckBoxArray.Remove(checkAllIndex); 
        CheckAllWasChecked = true; 
       } 
      } 
      for (int i = 0; i < GridView1.Rows.Count; i++) 
      { 
       if (GridView1.Rows[i].RowType == DataControlRowType.DataRow) 
       { 
        CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("CheckBox1"); 
        CheckBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1); 
        if (chk.Checked) 
        { 
         if (CheckBoxArray.IndexOf(CheckBoxIndex) == -1 && !CheckAllWasChecked) 
         { 
          CheckBoxArray.Add(CheckBoxIndex); 
         } 
        } 
        else 
        { 
         if (CheckBoxArray.IndexOf(CheckBoxIndex) != -1 || CheckAllWasChecked) 
         { 
          CheckBoxArray.Remove(CheckBoxIndex); 
         } 
        } 
       } 
      } 
     } 
     ViewState["CheckBoxArray"] = CheckBoxArray; 

     GridView1.DataBind(); 
    } 

    protected void OnPaging1xxx(object sender, GridViewPageEventArgs e) 
    { 

     GridView1.PageIndex = e.NewPageIndex; 
     GridView1.DataBind(); 
     if (ViewState["CheckBoxArray"] != null) 
     { 
      ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"]; 
      string checkAllIndex = "chkAll-" + GridView1.PageIndex; 

      if (CheckBoxArray.IndexOf(checkAllIndex) != -1) 
      { 
       CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[4].FindControl("chkAll"); 
       chkAll.Checked = true; 
      } 
      for (int i = 0; i < GridView1.Rows.Count; i++) 
      { 

       if (GridView1.Rows[i].RowType == DataControlRowType.DataRow) 
       { 
        if (CheckBoxArray.IndexOf(checkAllIndex) != -1) 
        { 
         CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("CheckBox1"); 
         chk.Checked = true; 
         GridView1.Rows[i].Attributes.Add("style","background-color:aqua");  
        } 
        else 
        { 
         int CheckBoxIndex = GridView1.PageSize * (GridView1.PageIndex) + (i + 1); 
         if (CheckBoxArray.IndexOf(CheckBoxIndex) != -1) 
         { 
          CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("CheckBox1"); 
          chk.Checked = true; 
          GridView1.Rows[i].Attributes.Add("style", "background-color:aqua"); 
         } 
        } 
       } 
      } 
     } 
    } 
    protected void RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)"); 
      e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)"); 
     } 
    } 
} 

它是不能夠堅持的價值觀的代碼,但是當我調試checkboxarray和viewstate["checkboxarray"]似乎擁有正確的價值觀。我不明白爲什麼它不起作用。請幫幫我。預計感謝您

回答

1

請注意,ViewState僅在從服務器到瀏覽器的旅程中「起作用」。你不能指望它在帖子後面仍然存在。

堅持它需要使用會話。

要回到你需要或者把它放在一個表格
服務器編輯

任何東西 - >這將是在的Request.Form [「布拉布拉」]
或在查詢字符串(mypage.aspx布拉布拉= someValue中?)
- >它會在的Request.QueryString [「布拉布拉」]

這些是僅有的兩個方法來從服務器獲取數據到您的網頁。 。

(附錄:

這是稍微隱藏你當你控制觸發你在你的代碼隱藏文件的CS處理事件ASP.NET包裹的控件的形式和做了回發 - >您運行你的代碼 - >更新後的頁面被髮回)

+0

這不是問題,在.aspx頁面中的OnPageIndexChanging =「OnPaging1xxx」代碼似乎有問題。我認爲這是使特定的複選框被選中,但我認爲還有其他一些事件/方法正在使頁面失去啓用複選框的值。如果您可以在頁面生命週期中給我更詳細的圖片,可能有助於破譯這個難題。 – 2011-03-15 16:36:32