Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,
I have Header and Item CheckBox inside a asp:Gridview as follows.
ASP.NET
<Columns>
<asp:TemplateField HeaderText="CheckAll">
	<HeaderStyle HorizontalAlign="Left" Width="2%" />
	<HeaderTemplate>
		<asp:CheckBox ID="cbxHdrPresent" onclick="javasrcipt:checkAllBoxes(this);"
		runat="server" Style="cursor: pointer;" Width="10" />
	</HeaderTemplate>
	<ItemTemplate>
		<asp:CheckBox ID="cbxItmPresent" AutoPostBack="false" EnableViewState="true" ViewStateMode="Enabled" Enabled="true" runat="server" />
	</ItemTemplate>
	<ControlStyle Width="4px" />
	<FooterStyle HorizontalAlign="Left" />
</asp:TemplateField>
....

And,
Using the follwing Javascript, check boxes will be checked/unchecked.
JavaScript
function checkAllBoxes(headerchk) {
            //var gvcheck = document.getElementById('GridView1');
            var gvcheck = document.getElementById('<%=gvPresent.ClientID%>');
            var i, j;

            var inputs = gvcheck.getElementsByTagName('input');
            j = inputs.length;
            //Condition to check header checkbox selected or not if that is true checked all checkboxes
            if (headerchk.checked) {
                for (i = 0; i < j; i++) {
                    inputs[i].Check = true;
                    inputs[i].style.backgroundColor = '#F5B325';
                }
            }
            //if condition fails uncheck all checkboxes in gridview
            else {
                for (i = 0; i < j; i++) {
                    inputs[i].checked = false;
                    inputs[i].removeAttribute("style");
                }
            }
        }

C#
public void ProcessDailyAttendance(string status,GridView GridViewControl,DateTime AttendanceDate)
  {
   foreach (GridViewRow grdRow1 in GridViewControl.Rows)
     {
      CheckBox chkBoxPresent = (CheckBox)GridViewControl.Rows[grdRow1.RowIndex].FindControl("cbxItmPresent");
    if (chkBoxPresent.Checked == true)     <<-- This returns FALSE even if CHECKED ->>
    {
       ....
    }
}

The same codes are working fine in live.
Now we upgrading this page with master pages. Is there any conflicts, working with master page.?
Please help me out.
Thanks,
Rajan.
Posted
Updated 29-Aug-13 0:09am
v5

use this code and check and make sure that the gridview is not loaded again after postback.
C#
for (int i = 0; i < gvBrowse.Rows.Count; i++)
{
     CheckBox chk = (CheckBox)gvBrowse.Rows[i].FindControl("cbxItmPresent");
     if (chk.Checked)
     {
     }
}
 
Share this answer
 
Comments
NPSSR 29-Aug-13 6:04am    
hi,
i missed to paste the "foreach" block. it's inside the "foreach" block only. I have updated my question. please refer.
NPSSR 29-Aug-13 7:25am    
Yes, Correct. I am loading the grid from many places. One of the place is occurred before this process. Found it and corrected. Many thanks.
Yes there will be conflicts with your current code when you include the master page
Now the current page html code surrounded with the ContentPlaceHolder1.

You need to change the javascript function for checkallBoxes to suit with this changes.

function should include like this

var RowCount=document.getElementById('ctl00_ContentPlaceHolder1_gvPresent').rows.length;

if the page use master page's ContentPlaceHolderID.
VB
<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="Server">
 
Share this answer
 
Comments
NPSSR 29-Aug-13 7:20am    
therefore, i have used "<%=gvPresent.ClientID%>".

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