<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
<script type="text/javascript">
function selectAll(invoker)
{
var inputElements = document.getElementsByTagName('input');
for (var i = 0 ; i < inputElements.length ; i++)
{
var myElement = inputElements[i];
if (myElement.type === "checkbox")
{
myElement.checked = invoker.checked;
}
}
return true;
}
</script>
<title>DataGridView Page</title>
</head>
<body>
<form id="form1" runat="server">
<h2 style="color: blue; font-style: normal">
Gridview checkBox
<br />
in asp.net
</h2>
<div>
<table>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button ID="btndel" runat="server" Text="Delete" OnClick="btndel_Click" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView runat="server" ID="gridviewchk" AutoGenerateColumns="False" AllowPaging="true"
OnPageIndexChanging="gridviewchk_PageIndexChanging" DataKeyNames="UserID">
<Columns>
<asp:TemplateField HeaderText="ALL">
<HeaderTemplate>
<asp:CheckBox ID="CheckBoxAll" runat="server" Text="ALL" onClick=" return selectAll(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
<asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
<asp:BoundField DataField="counts" HeaderText="count" SortExpression="count" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDetails"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
bind();
}
protected void btndel_Click(object sender, EventArgs e)
{
ArrayList a = new ArrayList();
for (int i = 0; i < gridviewchk.Rows.Count; i++)
{
CheckBox chkbx = (CheckBox)gridviewchk.Rows[i].Cells[0].FindControl("CheckBox1");
if (chkbx.Checked)
{
a.Add(gridviewchk.Rows[i].Cells[1].Text);
}
}
deleterecord(a);
bind();
}
public void bind()
{
SqlCommand cmd = new SqlCommand("select * from login", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gridviewchk.DataSource = ds;
gridviewchk.DataBind();
}
public void deleterecord(ArrayList ar)
{
con.Open();
foreach(object i in ar)
{
string str="delete from login where UserID='"+i+"'";
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
con.Close();
}
protected void gridviewchk_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridviewchk.PageIndex = e.NewPageIndex;
bind();
}
private void RememberOldValues()
{
ArrayList users = new ArrayList();
string userid = null;
foreach (GridViewRow row in gridviewchk.Rows)
{
userid= (string)gridviewchk.DataKeys[row.RowIndex].Value;
CheckBox c = row.FindControl("CheckBox1") as CheckBox;
bool result = c.Checked;
if (Session["CHECKED_ITEMS"] != null)
users = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!users.Contains(userid))
users.Add(userid);
}
else
users.Remove(userid);
}
if (users != null && users.Count > 0)
Session["CHECKED_ITEMS"] = users;
}
private void RePopulateValues()
{
ArrayList users = (ArrayList)Session["CHECKED_ITEMS"];
if (users != null && users.Count > 0)
{
foreach (GridViewRow row in gridviewchk.Rows)
{
string userid = (string)gridviewchk.DataKeys[row.RowIndex].Value;
if (users.Contains(userid))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
myCheckBox.Checked = true;
}
}
}
}
}


No comments:
Post a Comment