标头复选框未选中“网格"视图中的所有复选框 [英] Header checkbox not checking all check boxes in Grid view

查看:89
本文介绍了标头复选框未选中“网格"视图中的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的问题是我无法通过Java脚本来实现功能,其中在标头模板中的复选框上进行选中将选中项目模板中的所有复选框.标头复选框的ID为"chkAll",项目检查bx为"chkEmail_Single".




Hi,
my prob is that I am unable to make a functionality through java script where checking on checkbox in header template will check my all check boxes in item template. The id of header checkbox is "chkAll" and item check bx is "chkEmail_Single".




function SelectAll() {
        var frm = document.forms[0];
        

      if(frm.getElementById("chkAll").checked==true )
        {
           if(frm.getElementById("chkEmail_Single").checked==false)
            {
                frm.getElementById("chkEmail_Single").checked();
            }
        }
        else
        {
              frm.getElementsById("").checked=false;
        }
       }

推荐答案


检查此示例
很好,希望对您有帮助
Hi ,
Check this Example
It work Fine Hope it help
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function SelectAll(id) {
            var frm = document.forms[0];

            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox") {
                    if (frm.elements[i].disabled == true) {
                        frm.elements[i].checked = false;
                    }
                    else {
                        frm.elements[i].checked = document.getElementById(id).checked;
                    }
                }
            }
        }
        function unCheck(id) {
            var frm = document.forms[0];
            var paren = document.getElementById(id);
            var flag = true;
            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox") {
                    if (!frm.elements[i].checked) {
                        paren.checked = false;
                        if (frm.elements[i] != paren)
                            flag = false;
                    }
                }
            }
            if (flag == true) paren.checked = true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="Gridview1" runat="server"

                        AutoGenerateColumns="False"

                    OnRowDataBound="Gridview1_RowDataBound" AllowPaging="True" >
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="Chk" runat="server"/>
                                </ItemTemplate>
                                <HeaderTemplate>
                                    <asp:CheckBox ID="Chk1" runat="server" />
                                </HeaderTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="test">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Val" runat="server" Text='<%# Eval("name") %>' ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
     <asp:HiddenField ID="HF2" runat="server" />
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>





DataClassesDataContext db = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
    //binding to Grid
    if (!IsPostBack)
    {
        var result = from x in db.test1s
                     select new { x.name };
        Gridview1.DataSource = result;
        Gridview1.DataBind();
    }

}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.Header))
    {
        HF2.Value = ((CheckBox)e.Row.FindControl("Chk1")).ClientID;
        //adding an attribut for onclick event on the check box in the hearder and passing the ClientID of the Select All checkbox
        ((CheckBox)e.Row.FindControl("Chk1")).Attributes.Add("onclick",
            "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("Chk1")).ClientID + "')");
    }
    else if ((e.Row.RowType == DataControlRowType.DataRow))
    {
        ((CheckBox)e.Row.FindControl("Chk")).Attributes.Add("onclick",
            "javascript:unCheck('" + HF2.Value + "','" + ((CheckBox)e.Row.FindControl("Chk")).ClientID + "')");

    }

}
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in Gridview1.Rows)
    {
        if ((CheckBox)row.FindControl("Chk") is CheckBox)
        {
            if (((CheckBox)row.FindControl("Chk")).Checked == true)
            {
                Response.Write("<script>alert('" + ((Label)row.FindControl("lbl_Val")).Text + "')</script>");
            }
        }
    }

}



最好的问候
M.Mitwalli



Best Regards
M.Mitwalli



请通过以下链接:
选择/取消选择GridView内部的所有复选框 [ ^ ]

您的问题将得到解决.

祝一切顺利.
-AK
Hi,
Please go through this link:
Selecting / Deselecting all the CheckBoxes Inside a GridView[^]

Your problem will be solved.

All the best.
-AK


window.onload = function()
{
   //Get total no. of CheckBoxes in side the GridView.
   TotalChkBx = parseInt('<%= this.gvAllClients.Rows.Count %>');*******<- error here

   //Get total no. of checked CheckBoxes in side the GridView.
   Counter = 0;
}

function HeaderClick(CheckBox)
{
   //Get target base & child control.
   var TargetBaseControl = 
       document.getElementById('<%= this.gvAllClients.ClientID %>');
   var TargetChildControl = "chkEmail_Single";

   //Get all the control of the type INPUT in the base control.
   var Inputs = TargetBaseControl.getElementsByTagName("input");*****<----error than here

   //Checked/Unchecked all the checkBoxes in side the GridView.
   for(var n = 0; n < Inputs.length; ++n)
      if(Inputs[n].type == 'checkbox' && 
                Inputs[n].id.indexOf(TargetChildControl,0) >= 0)
         Inputs[n].checked = CheckBox.checked;

   //Reset Counter
   Counter = CheckBox.checked ? TotalChkBx : 0;
}

function ChildClick(CheckBox, HCheckBox)
{
   //get target control.
   var HeaderCheckBox = document.getElementById(HCheckBox);

   //Modifiy Counter; 
   if(CheckBox.checked && Counter < TotalChkBx)
      Counter++;
   else if(Counter > 0) 
      Counter--;

   //Change state of the header CheckBox.
   if(Counter < TotalChkBx)
      HeaderCheckBox.checked = false;
   else if(Counter == TotalChkBx)
      HeaderCheckBox.checked = true;
}


 protected void btnEmail_Click(object sender, EventArgs e)
    {

        for (int ict = 0; ict < gvAllClients.Rows.Count; ict++)
        {
            CheckBox check = ((CheckBox)(gvAllClients.Rows[ict].FindControl("chkEmail_Single")));
            if (check.Checked = true)
            {
                string strDatatype = gvAllClients.Rows[ict].Cells[5].Text.ToString();
            }
        }
        }




选择之后,我想对选中其复选框的行执行一些操作,但是这需要我点击行...请参见上文:




After selection i want to perform some action on the row whose check box is being selected but it takes me to astriek line...see above :


这篇关于标头复选框未选中“网格"视图中的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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