Click here to Skip to main content
15,888,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm have some problem with CheckBox in GridView with paging control...

I'm having one GridView with CheckBox and paging control...
When I'm selecting the CheckBox in page one and selecting the one CheckBox in paging 2 and soon..



I need sum of how many CheckBoxes I selected in the GridView with paging control..

May anyone help me from this problem please I'm trying from 2 days onwards...
Posted
Updated 28-Aug-17 23:14pm
v2
Comments
Nelek 29-Mar-12 8:19am    
And what have you tried so far? Post a bit of your code, so people can tell you where the error is. That will help you to learn.

C#
protected void GridView1_PreRender(object sender, EventArgs e)
 {

     if (Session["page" + GridView1.PageIndex] != null)
     {
         int count = Convert.ToInt32(ViewState["total"]);
         CheckBox chb;
         bool[] values = (bool[])Session["page" + GridView1.PageIndex];

         for (int i = 0; i < GridView1.Rows.Count; i++)
         {
             chb = (CheckBox)GridView1.Rows[i].FindControl("chkStatus");

             chb.Checked = values[i];

         }

     }
 }





protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{


Response.Write(GridView1.PageIndex.ToString());
int d = GridView1.PageCount;
bool[] values = new bool[GridView1.PageSize];

CheckBox chb;
int count = Convert.ToInt32(ViewState["total"]);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
chb = (CheckBox)GridView1.Rows[i].FindControl("chkStatus");

if (chb != null)
{
values[i] = chb.Checked;
count++;
}
}
Session["page" + GridView1.PageIndex] = values;





GridView1.PageIndex = e.NewPageIndex;
DataTable dtPagination = ((DataTable)Session["details"]);
DataView dvPagination = new DataView(dtPagination);
GridView1.DataSource = dtPagination;
GridView1.DataBind();

}


i have use these two events i can check the checkbox in the paging but i want to know how many check boxes i have checked overall gridview with paging control.....
 
Share this answer
 
v3
Comments
bbirajdar 30-Mar-12 3:17am    
Your code is wrong

It should be like this

<asp:TemplateField HeaderText="View">
<itemtemplate>
<asp:CheckBox ID="chkview" runat="server" AutoPostBack="true" OnCheckedChanged="chkview_CheckedChanged" />




protected void chkview_CheckedChanged(object sender, EventArgs e)
{
if((sender as CheckBox).Checked==true)
{
Convert.ToInt32(Session["counter"])++;
}
else
{
Convert.ToInt32(Session["counter"])--;
}
}
surikarthi 30-Mar-12 3:45am    
sir if i'm using your code also its counting the checked values again n again..

for example..
Paging index 1 in grid view i checked 1 check box and i went to next paging index in grid view to select other check box... here total =2 its correct..

But when i'm coming back to the paging index 1 to check another checkbox actually it should show total=3 but its showing total =4... means its counting again the previous checked checkbox..
bbirajdar 30-Mar-12 5:23am    
"But when i'm coming back to the paging index 1 to check another checkbox actually it should show total=3 but its showing total =4... means its counting again the previous checked checkbox.." .. Ok.. I read this point carefully. Does this mean that the chkview_CheckedChanged event is fired on page index changing also? Then this is wierd. Ideally this should not happen. A checkbox event is fired only on a check/uncheck. Hint: Please check in the aspx if this chek_checked event is called from from any other place by mistake
bbirajdar 30-Mar-12 5:28am    
When you go back to the previous page, the GridView1_PageIndexChanging event is fired and will again count the checked items, which were already counted. Because you have written the counting code in GridView1_PageIndexChanging event.No matter it will coun it again and again and again every time GridView1_PageIndexChanging is fired.
Counting logic should be actually in chekbox_checkedChanged event.
surikarthi 30-Mar-12 5:41am    
ha i have removed from the GridView1_PageIndexChanging and i used counting in the chekbox_checkedChanged event only.. then also same problem..
Maintain a counter of checkboxes checked into a session variable. This session variable should be updated for every checkbox checked and unchecked.

Edit:

On the checkbox_checked event on server side , update the counter maintained in the session variable. This event is fired on every checkbox checked-unchecked.
 
Share this answer
 
v2
Comments
surikarthi 30-Mar-12 0:09am    
its updating the check box whether it is selected or not but i need count of checked items... , its counting again and again the selected items...
bbirajdar 30-Mar-12 1:00am    
Did you use a session variable that will be incremented or decremented after every check-uncheck?
surikarthi 30-Mar-12 1:34am    
i have under lined the code where i have used seesion in the code for increment of the check box count in the above code which i posted before...

but due to this its counting the checkbox checked items again n again...

For example,
when i press paging index 1-- its counting the checkbox which is checked..
and when i press paging index 2 -- there also its counting correctly..
but my problem is when i'm going back to paging 1 to check another check box that time its counting again the previous checked checkbox...
surikarthi 30-Mar-12 2:51am    
please any one going to give me suggestion on this issue...
bbirajdar 30-Mar-12 5:19am    
That is because you have written the logic for counter updation in the GridView1_PageIndexChanging event. If you has written the same in the Checkbox_Checked event do you think this counting logic will be executed on GridView1_PageIndexChanging event ? Certainly not.

The counting logic SHOULD be in checkbox_checked event and SHOULD NOT be in GridView1_PageIndexChanging event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900