First Design The Page like following way take one Gridview and One Button for Delete
The Following is .aspx page design
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox2_CheckedChanged"
/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1"
runat="server"
Text="Delete"
onclick="Button1_Click"
/>
Design is as follow
Now in .aspx.cs file write the following code...
For delete selected record. . .
protected void Button1_Click(object
sender, EventArgs e)
{
foreach
(GridViewRow rw in
GridView1.Rows)
{
CheckBox
chk = (CheckBox)rw.FindControl("CheckBox1");
if
(chk.Checked)
{
con.Open();
Label
id = (Label)rw.FindControl("Label1");
SqlCommand
cmd = new SqlCommand("delete from cust where id='" + id.Text
+ "'", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
grid();
}
For delete All record...
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox
chk = (CheckBox)sender;
GridViewRow
rw = (GridViewRow)chk.Parent.Parent;
CheckBox
c1 = (CheckBox)rw.FindControl("CheckBox2");
if
(c1.Checked)
{
for
(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox
c2 = GridView1.Rows[i].FindControl("CheckBox1")
as CheckBox;
//CheckBox
c2 = (CheckBox)rw.FindControl("CheckBox1");
c2.Checked = true;
}
//Response.Write("hiiiiiiiii...");
}
else
{
for
(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox
c2 = GridView1.Rows[i].FindControl("CheckBox1")
as CheckBox;
//CheckBox
c2 = (CheckBox)rw.FindControl("CheckBox1");
c2.Checked = false;
}
}
}
Gridview bind function...
protected void grid()
{
//Here create the connection . . .
con.Open();
SqlDataAdapter
adp = new SqlDataAdapter("select * from cust", con);
DataTable
dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
If you have face any error then comment me. . .
5 comments:
but wat is that label id here
Label id = (Label)rw.FindControl("Label1");
Dude here i'm deleting a record on the base of ID so on the Label1 Control i display the ID so i gave the name id to label object for fetch a label value.
If you not understand please ask me again. . .Thanks for Reading this.
CheckBox chk = (CheckBox)rw.FindControl("CheckBox1");
after this if(chk.checked) shows false but i am selected in the form
Please Mariappan see carfully there no mistake of Controls Name, and it's not then please send me your block of code lines in which you geting this, by post comment.
Very informative post..Asp.Net Training Mumbai
Post a Comment