我在c#中有一个代码,在标题文本中有一个复选框,我点击下面的文本框应该点击复选框

时间:2014-08-08 10:10:39

标签: c# asp.net

我有一个代码,在标题文本中有一个复选框,当我点击标题复选框时,所有复选框都应该有效,但事实并非如此。请找到下面的代码和我附上的图片。

谢谢!

checkboxclick

ASPX代码

<%@ 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");







        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个例子,可以帮助你。

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