tags:

views:

21

answers:

2

In a grid view i have used paging for that i have used the view state to store datatale to bind it on GridView1_PageIndexChanging event every thing works fine but the problem happens with the first column which is having the checkbox placed in each row . On navigation all checked check box becomes unchecked how to maintain the state of check box as well.

this is the aaspx code

<Columns>
<asp:TemplateField HeaderText="Select Student">
<ItemTemplate>
<asp:CheckBox id="Chek"  runat="server" Text="select" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Enrollment No." DataField="enrollment_no" />
<asp:BoundField HeaderText="Course Name" DataField="course_name"/>
<asp:BoundField  HeaderText="Branch Name"  DataField="branch_name"/>
<asp:BoundField HeaderText="Email Id" DataField="email" />
<asp:BoundField  HeaderText="Mobile" DataField="mobile"/>
<asp:BoundField HeaderText="Name"  DataField="first_name"/>
<asp:BoundField  HeaderText="Surname" DataField="last_name" />
</Columns>


</asp:GridView>
A: 

Viewstate is intended for postbacks to the same page.

To preserve state when navigating to other pages here are 3 options:

  1. Put your checkbox (or simply true/false) in the Session
  2. Use the PreviousPage property
  3. Or use cookies

Summary of option #2

If you have ot post accross pages, cookies can be used, and also Cross Page Posting by setting the PostBackURL property of a button, then the POST request is directed at the specified page, and you can get the values from the PreviousPage property of the next page.

Example of using option #3, the Session:

//Set
Session["mySessionVariableName"] = myCheckBox;

//Get
CheckBox myCheckBox = (CheckBox)Session["mySessionVariableName"];

I summarized in more detail here and here

GenEric35
i just want to do it for single page, but it's about grid view so how can i loop for all the rows and find all the checked check boxes and store it in view state and bind again to check boxes on paging to retain all check boxes state
NoviceToDotNet
please just code me how should i store all check box state for each row grid view in view state and on what event how should i reassign the state of each checkbox again.
NoviceToDotNet
Have you tried adding EnableViewState, like this: <asp:CheckBox id="Chek" runat="server" Text="select" EnableViewState="true" ></asp:CheckBox>"
GenEric35
yes viewstate is true in grid view on paging to next it lost the chec box actual state all becomes unselected even selected as well.
NoviceToDotNet
A: 

Do u need this type of application

     http://www.highoncoding.com/Articles/697_Persisting_CheckBox_State_While_Paging_in_GridView_Control.aspx
Dorababu