Tuesday, February 1, 2011

 Gridview Check all row C# and rember checked checkboxes after postback

 Gridview Check all row C# and rember checked checkboxes after postback
 
 
 <asp:GridView ID="gv" runat="server" Visible="false"></asp:GridView>

<asp:GridView ID="GridView1" runat="server" DataKeyNames="id"

AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"

AutoGenerateEditButton="True"

OnRowCancelingEdit="gdview_RowCancelingEdit" AutoGenerateDeleteButton="True"

OnRowDeleting="gdview_RowDeleting" AllowSorting="True" OnSorting ="grdView_OnSorting"

OnRowEditing="gdview_RowEditing" OnRowUpdating="gdview_RowUpdating"

CellPadding="4" ForeColor="#333333" GridLines="None"

>

<AlternatingRowStyle BackColor="White" ForeColor="#284775" />

<Columns>

<asp:TemplateField HeaderText="Select">

<HeaderTemplate>

<!--<asp:CheckBox ID="chkSelectAll" runat="server" Text="Select&nbsp;All" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />-->

<label id="heda" text="Select"></label>

</

HeaderTemplate>

<

ItemTemplate>

<

asp:CheckBox ID="CheckBoxVid" runat="server" />

</

ItemTemplate>

</

asp:TemplateField>

<asp:BoundField DataField="video_id" HeaderText="video_id" SortExpression="video_id" />

<

asp:BoundField DataField="content_type" HeaderText="content_type" SortExpression="content_type" />

<

asp:BoundField DataField="policy" HeaderText="policy" SortExpression="policy" />

<

asp:BoundField DataField="video_title" HeaderText="video_title" SortExpression="video_title" />

<

asp:BoundField DataField="video_duration" HeaderText="video_duration" SortExpression="video_duration" />

<

asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />

<

asp:BoundField DataField="uploader" HeaderText="uploader" SortExpression="uploader" />

<

asp:BoundField DataField="claim_type" HeaderText="claim_type" SortExpression="claim_type" />

<

asp:BoundField DataField="claim_origin" HeaderText="claim_origin" SortExpression="claim_origin" />

<

asp:BoundField DataField="embed_views" HeaderText="embed_views" SortExpression="embed_views" />

<

asp:BoundField DataField="watch_views" HeaderText="watch_views" SortExpression="watch_views" />

<

asp:BoundField DataField="share_rev" HeaderText="share_rev" SortExpression="share_rev" />

<

asp:BoundField DataField="partner_sold_revenue" HeaderText="partner_sold_revenue" SortExpression="partner_sold_revenue" />

<

asp:BoundField DataField="afv_revenue" HeaderText="afv_revenue" SortExpression="afv_revenue" />

<

asp:BoundField DataField="amount_payable" HeaderText="amount_payable" SortExpression="amount_payable" />

<

asp:BoundField DataField="has_multiple_claims" HeaderText="has_multiple_claims" SortExpression="has_multiple_claims" />

<

asp:BoundField DataField="category" HeaderText="category" SortExpression="category" />

<

asp:BoundField DataField="custom_id" HeaderText="custom_id" SortExpression="custom_id" />

<

asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />

</Columns>

<EditRowStyle BackColor="#999999" />

<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

<SortedAscendingCellStyle BackColor="#E9E7E2" />

<SortedAscendingHeaderStyle BackColor="#506C8C" />

<SortedDescendingCellStyle BackColor="#FFFDF8" />

<SortedDescendingHeaderStyle BackColor="#6F8DAE" />

</asp:GridView>

</

ContentTemplate>

</

asp:UpdatePanel>

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)

{

CheckBox cbHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");

chkheadbx = cbHeader.Checked.ToString();

// Session["chxboxhd"] = chkheadbx;

// Response.Write(Session["chxboxhd"]);

CheckBox chk;

foreach (GridViewRow rowItem in GridView1.Rows)

{

chk = (

CheckBox)(rowItem.Cells[0].FindControl("CheckBoxVid"));

chk.Checked = ((

CheckBox)sender).Checked;

}

Session[

"chxboxhd"] = chkheadbx;

// Response.Write(Session["chxboxhd"]);

}

 

 

private void RememberOldValues()

{

ArrayList categoryIDList = new ArrayList();

int index = -1;

foreach (GridViewRow row in GridView1.Rows)

{

index = (

int)GridView1.DataKeys[row.RowIndex].Value;

bool result = ((CheckBox)row.FindControl("CheckBoxVid")).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 RePopulateValues()

{

ArrayList categoryIDList = (ArrayList)Session[CHECKED_ITEMS];

if (categoryIDList != null && categoryIDList.Count > 0)

{

foreach (GridViewRow row in GridView1.Rows)

{

int index = (int)GridView1.DataKeys[row.RowIndex].Value;

if (categoryIDList.Contains(index))

{

CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBoxVid");

myCheckBox.Checked =

true;

}

}

}

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

RememberOldValues();

GridView1.PageIndex = e.NewPageIndex;

BindData();

RePopulateValues();

}

 

 

No comments: