//source file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chekbox.aspx.cs" Inherits="chekbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
border-collapse: collapse;
border-left-style: solid;
border-left-width: 1px;
border-right: 1px solid #C0C0C0;
border-top-style: solid;
border-top-width: 1px;
border-bottom: 1px solid #C0C0C0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="style1">
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="check">
<%--<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
</EditItemTemplate>--%>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged"
AutoPostBack="True" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chekbox.aspx.cs" Inherits="chekbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
border-collapse: collapse;
border-left-style: solid;
border-left-width: 1px;
border-right: 1px solid #C0C0C0;
border-top-style: solid;
border-top-width: 1px;
border-bottom: 1px solid #C0C0C0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="style1">
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="check">
<%--<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
</EditItemTemplate>--%>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged"
AutoPostBack="True" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
//code page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using jayModel;
using System.EnterpriseServices;
using System.Data.EntityModel;
public partial class chekbox : System.Web.UI.Page
{
jayEntities j = new jayEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
var d = j.spselect();
GridView1.DataSource = d;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
//protected void chkall_CheckedChanged(object sender, EventArgs e)
//{
// //CheckBox ch2 = (CheckBox)GridView1.HeaderRow.FindControl("chkall");
// //if (ch2.Checked)
// //{
// // foreach (GridViewRow row in GridView1.Rows)
// // {
// // CheckBox ch1 = (CheckBox)row.FindControl("CheckBox1");
// // ch1.Checked = true;
// // }
// //}
// //else
// //{
// // foreach (GridViewRow row in GridView1.Rows)
// // {
// // CheckBox ch1 = (CheckBox)row.FindControl("CheckBox1");
// // ch1.Checked = false;
// // }
// //}
//}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox ch2 = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");
if (ch2.Checked)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ch1 = (CheckBox)row.FindControl("CheckBox1");
ch1.Checked = true;
}
}
else
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ch1 = (CheckBox)row.FindControl("CheckBox1");
ch1.Checked = false;
}
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ch1 = (CheckBox)row.FindControl("CheckBox1");
if (ch1.Checked)
{
int id1 = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
var l = j.Table1.Where(n => n.id ==id1).Single<Table1>();
j.DeleteObject(l);
j.SaveChanges();
bind();
}
}
}
}
No comments:
Post a Comment