简体   繁体   中英

I have a code in c# were in header text there is a checkbox and i click on the text box below check boxes should be clicked

I have a code where in header text there is a checkbox and when i click on the header check box all the check box should work, but it is not. Please find the code below and images i have attached.

Thank you!

checkboxclick

ASPX code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <div>
        <asp:GridView ID="gvEmployeeDetails" runat="server" CellPadding="4" Font-Bold="True"
        Font-Size="Large" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

    <Columns>
        <asp:TemplateField HeaderText="Delete All">

            <HeaderTemplate>
               <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true"  OnCheckedChanged="column1" />

            </HeaderTemplate>
           <ItemTemplate>
               <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="column2" />


           </ItemTemplate>

        </asp:TemplateField>




        <asp:BoundField DataField="empid" HeaderText="empid" SortExpression="empid" />
        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
        <asp:BoundField DataField="designation" HeaderText="designation" SortExpression="designation" />
        <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
        <asp:BoundField DataField="country" HeaderText="country" SortExpression="country" />
    </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:servion_hariConnectionString %>" SelectCommand="SELECT [empid], [name], [designation], [city], [country] FROM [employeedetails]"></asp:SqlDataSource>
        </form>
    </body>
    </html>

Aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Data.SqlClient;



namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        DataTable ds;
        DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["dtd"] = null;
                ds = new DataTable();
               string conn = ("Data Source=localhost;Database=hari;user=sa;pwd=Servion@123");
               string q = "select * from employeedetails";
               SqlConnection con = new SqlConnection(conn);
               SqlCommand cmd = new SqlCommand(q, con);
               SqlDataAdapter da = new SqlDataAdapter();
               con.Open();
               cmd.ExecuteNonQuery();

               da.SelectCommand = cmd;
               da.Fill(ds);
               //gvEmployeeDetails.DataSource = ds;
               gvEmployeeDetails.DataBind();
               Session["dtd"] = true;

            }


    }
        protected void column1(object sender, EventArgs e)
        {


            dt = Session["dtd"] as DataTable;
            CheckBox chkAll = (CheckBox)gvEmployeeDetails.HeaderRow.FindControl("chkSelectAll");
            if (chkAll.Checked == true)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CheckBox c = (CheckBox)gvEmployeeDetails.Rows[i].FindControl("CheckBox1");

                    c.Checked = true;

                }

            }
            else
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    CheckBox c = (CheckBox)gvEmployeeDetails.Rows[i].FindControl("CheckBox1");

                    c.Checked = false;




                }




            }




        }
        protected void column2(object sender, EventArgs e)
        {

            CheckBox btndetails = sender as CheckBox;
           GridViewRow row = (GridViewRow)btndetails.NamingContainer;
           CheckBox li = (CheckBox)row.FindControl("CheckBox1");







        }
    }
}

here is an example,may help you.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    //customized global checkbox  in header
    int iCheckBoxColumnIndex = 7; // index of check box cell
    bool bValue = checkBox1.Checked;
    for (int iRow = 0; iRow < dataGridView1.Rows.Count; iRow++)
    {
        if (iRow == dataGridView1.NewRowIndex)
            continue;
        dataGridView1.Rows[iRow].Cells[iCheckBoxColumnIndex].Value = bValue;
        checkValue(iRow, bValue);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM