Monday, November 17, 2008

Select All And Delete Using CheckBox In GridView -Client Side

0 comments 11/17/2008
Sign up to receive our site updates!

In Coding Page



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

<title>Untitled Pagetitle>

<script type="text/javascript">

//javascript function for select and deselect all check boxes

function SelectAll(status)

{

var gridview = document.getElementById("GridView1");

for(var i = 1;i < gridview.rows.length; i++) { gridview.rows[i].cells[1].firstChild.checked = status; } }


script>

head>

<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="btndelete" runat="server" OnClick="btndelete_Click" Style="z-index: 100;

left: 227px; position: absolute; top: 110px" Text="DeleteAll" />

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

Style="z-index: 101; left: 225px; position: absolute; top: 140px" AutoGenerateColumns="False" DataKeyNames="eno" AutoGenerateDeleteButton="True">

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#EFF3FB" />

<EditRowStyle BackColor="#2461BF" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

<Columns>

<asp:TemplateField >

<HeaderTemplate>

<asp:CheckBox ID="chkall" runat="server" onclick="javascript:SelectAll(this.checked);" />

<%-- Onclick event is used to call Java Script Function--%>

HeaderTemplate>

<ItemTemplate>

<asp:CheckBox ID="chk1" runat="server" />

ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle HorizontalAlign="Left" />

asp:TemplateField>

<asp:BoundField DataField="eno" HeaderText="EmpNo" ReadOnly="True" SortExpression="eno" />

<asp:BoundField DataField="ename" HeaderText="ename" SortExpression="ename" />

<asp:BoundField DataField="sal" HeaderText="sal" SortExpression="sal" />

Columns>

asp:GridView>

<asp:CheckBox ID="CheckBox1" runat="server" Style="z-index: 103; left: 511px; position: absolute;

top: 175px" />

div>

form>

body>

html>

Set Following GridView Properties

DatakeyNames=eno(set Primary Key colume name)



In .CS File



using System.Data.Odbc;



public partial class _Default : System.Web.UI.Page

{

string str;

OdbcConnection myConn = new OdbcConnection("driver={Mysql odbc 3.51 driver};user id=root;password=admin;server=localhost;database=temp");

protected void Page_Load(object sender, EventArgs e)

{

btndelete.Attributes.Add("onclick", "return confirmDelete(this.form1);");



if (IsPostBack == false)

{

str = "select * from emp1";

getdata(str);



}



}

public void getdata(string s)

{

OdbcDataAdapter da = new OdbcDataAdapter(s, myConn);

DataSet ds = new DataSet();

da.Fill(ds, "raji");

GridView1.DataSource = ds;

GridView1.DataBind();

}





protected void btndelete_Click(object sender, EventArgs e)

{

string ds = "";

foreach (GridViewRow r in GridView1.Rows)

{

CheckBox cb = (CheckBox)r.FindControl("chk1");

if (cb.Checked)

{

int eno = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);

ds = "delete from emp1 where eno=" + eno;

OdbcCommand cmd = new OdbcCommand(ds, myConn);

myConn.Open();

cmd.ExecuteNonQuery();

myConn.Close();

}

}

getdata("select * from emp1");

}

}

Your Ad Here

0 comments:

Respects for your's Questions & Opinions

  • Friends
  •  

    Recent Posts

    New Comments

    Copyright 2008 All Rights Reserved Tech Tasks Template by Rajesh Kumar Chekuri

    Share
    Me
    Twitter Facebook Google +1 Pinterest Delicious Digg Stumbleupon More

    Connect to TT Facebook Google +1
    Back to Top