选中的复选框始终返回false [英] Checkbox checked always returns false

查看:102
本文介绍了选中的复选框始终返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在asp:Gridview中有Header和Item CheckBox,如下所示。

 <   >  
< asp:TemplateField HeaderText = CheckAll > ;
< HeaderStyle Horizo​​ntalAlign = 宽度 = 2% / >
< HeaderTemplate >
< asp:CheckBox ID = cbxHdrPresent onclick = javasrcipt:checkAllBoxes(this);

runat = server 样式 = cursor:pointer; 宽度 = 10 / >
< / HeaderTemplate >
< ItemTemplate >
< asp:CheckBox ID = cbxItmPresent AutoPostBack = false EnableViewState = true ViewStateMode = 已启用 < span class =code-attribute>已启用 = true runat = 服务器 / >
< / ItemTemplate >
< ControlSty le 宽度 = 4px / >
< FooterStyle Horizo​​ntalAlign = / >
< / asp:TemplateField >
....



并且,

使用以下Javascript,将选中复选框/未选中。

  function  checkAllBoxes(headerchk){ 
// var gvcheck = document.getElementById('GridView1');
var gvcheck = document .getElementById(' <%= gvPresent.ClientID%>');
var i,j;

var inputs = gvcheck.getElementsByTagName(' 输入);
j = inputs.length;
// 选中或不选中标题复选框的条件如果为真,则选中所有复选框
if (headerchk.checked){
for (i = 0 ; i< j; i ++){
inputs [i] .Check = true ;
inputs [i] .style.backgroundColor = ' #F5B325';
}
}
// 如果条件失败,请取消选中gridview中的所有复选框
else {
for (i = 0 ; i< j; i ++){
inputs [i] .checked = false ;
inputs [i] .removeAttribute( style);
}
}
}



  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 )<< - 返回FALSE甚至如果 CHECKED - >>
{
....
}
}



相同的代码在实时工作正常。

现在我们用母版页升级这个页面。是否有任何冲突,使用母版页。?

请帮帮我。

谢谢,

Rajan。

解决方案

使用此代码并检查并确保在回发后未再次加载gridview。

  for  int  i = < span class =code-digit> 0 ; i <  gvBrowse.Rows.Count; i ++)
{
CheckBox chk =(CheckBox)gvBrowse.Rows [i] .FindControl( cbxItmPresent);
if (chk.Checked)
{
}
}


是的,当你包含母版页时,你的当前代码会有冲突

现在当前的页面html代码被ContentPlaceHolder1包围。



您需要更改checkallBoxes的javascript函数以适应这种变化。



函数应该包含这样的



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



如果页面使用母版页的ContentPlaceHolderID。 br />

< asp:Content ID =   content1 ContentPlaceHolderID =   ContentPlaceHolder1 
runat = 服务器>


Hi all,
I have Header and Item CheckBox inside a asp:Gridview as follows.

<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.

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");
                }
            }
        }


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.

解决方案

use this code and check and make sure that the gridview is not loaded again after postback.

for (int i = 0; i < gvBrowse.Rows.Count; i++)
{
     CheckBox chk = (CheckBox)gvBrowse.Rows[i].FindControl("cbxItmPresent");
     if (chk.Checked)
     {
     }
}


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.

<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="Server">


这篇关于选中的复选框始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆