2011-03-30 21 views
2

私はグリッドビューでページングを使用しました。
私は、サーバー側またはクライアント側のどちらかでgridviewのチェック/チェックを外すためのコードが必要です。
私はページングなしで試してみました。私はページングでそれを必要とします。グリッドビューで処理されるページングでチェックしない/チェックしないでください

<asp:TemplateField> 
<HeaderTemplate> 
<asp:CheckBox ID="chkHGrid" runat="server" /> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:CheckBox ID="chkGrid" runat="server" /> 
</ItemTemplate> 
<ItemStyle HorizontalAlign="Center" /> 
<HeaderStyle HorizontalAlign="Center" /> 
</asp:TemplateField> 

答えて

4

このテクニックは、ページングでGridView CheckBoxの状態を維持すると呼ばれます。これを達成するための1つの方法は、以下の通りである。

  1. チェックインされた行に関連するデータキーを格納するintまたはstringのGenric Listを作成します。
  2. PageIndexを新しいインデックスに設定する前にPageIndexChangingを実行します。 GridViewの各行について
    • チェックボックスをオンにすると、idをジェネリックリストに格納します。
    • チェックボックスがオフになっている場合& IDがジェネリックリストに存在する場合は、リストから削除します。
  3. RowDataBoundイベントで、各DataRowに対して、DataKeyNameがGeneric Listに存在するかどうかを確認します。その場合は、CheckBoxを見つけてチェックします。

製品表と簡単な例は、Northwindデータベースは

private List<int> ProductIDs 
{ 
    get 
    { 
     if (this.ViewState["ProductIDs"] == null) 
     { 
      this.ViewState["ProductIDs"] = new List<int>(); 
     } 
     return this.ViewState["ProductIDs"] as List<int>; 
    } 
} 

protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    foreach (GridViewRow gvr in gvProducts.Rows) 
    { 
     CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox; 
     if (chkSelect != null) 
     { 
      int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]); 
      if (chkSelect.Checked && !this.ProductIDs.Contains(productID)) 
      { 
       this.ProductIDs.Add(productID); 
      } 
      else if (!chkSelect.Checked && this.ProductIDs.Contains(productID)) 
      { 
       this.ProductIDs.Remove(productID); 
      } 
     } 
    } 
} 

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    GridViewRow gvr = e.Row; 
    if (gvr.RowType == DataControlRowType.DataRow) 
    { 
     CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox; 
     if (chkSelect != null) 
     { 
      int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]); 
      chkSelect.Checked = this.ProductIDs.Contains(productID); 
     } 
    } 
} 
+0

それを行うにはどのように...任意のサンプルコードの背後にあるコード

の下にマークアップ

<asp:GridView ID="gvProducts" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ProductID" OnPageIndexChanging="gvProducts_PageIndexChanging" OnRowDataBound="gvProducts_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="sdsProducts" runat="server" ConnectionString='<%$ ConnectionStrings:NorthwindConnectionString %>' SelectCommand="SELECT * FROM Products"> </asp:SqlDataSource> 

を与えています,,, –

+0

datakeynamesはテンプレートfeild –

+0

にありますか?gridview.DataKeynames = "ProductID"のマークアップを確認してください。あなたはそれをitemtemplateの隠しフィールドとして格納することができます。それは私たちがソースを見て、それが良い習慣ではないときにidを公開します。 – naveen