in this program i am going show you how to delete record on checkbox changed event when you
have taken checkbox into gridview .
on check box checked record will be deleted.
protected void chkval_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender; //will make checked checkbox true
GridViewRow row = (GridViewRow)chk.NamingContainer; //will hit row
Boolean ischecked =((CheckBox)(row.FindControl("chkval"))).Checked ;
//will find checkbox which has selected or checked
if (ischecked)
{
txtnm.Text = ((Label)(row.FindControl("lblNm"))).Text;
delete(txtnm.Text);
}
else
{
}
bind();
}
now check for delete function from where we are passing value from checked event.
public void delete(string nm)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from stud where nm='"+nm+"'", con);
int i= cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
string deletevalue = "<script>alert(record deleted succesfully);</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", deletevalue,false);
}
else
{
}
}
now after click on checkbox value will be deleted from respective table and database.
<asp:GridView runat="server" ID="grd" AutoGenerateColumns ="false" >
<Columns >
<asp:TemplateField headertext ="Id">
<ItemTemplate >
<%#Eval("id")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Name">
<ItemTemplate >
<asp:Label runat="server" ID="lblNm" Text='<%#Eval("nm")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Select">
<ItemTemplate >
<%#Eval("lnm")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Select">
<ItemTemplate >
<asp:CheckBox ID="chkval" runat="server" AutoPostBack="true" OnCheckedChanged="chkval_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Lname">
<ItemTemplate >
<input type =text id="txtAdd" value ='<%# "Mr " +Eval("nm")+" "+ Eval("lnm") %>'/></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void bind()
{
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from stud", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
sda.Fill(ds);
ViewState["viewds"] = ds;
grd.DataSource = ds;
grd.DataBind();
con.Close();
}
have taken checkbox into gridview .
on check box checked record will be deleted.
protected void chkval_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender; //will make checked checkbox true
GridViewRow row = (GridViewRow)chk.NamingContainer; //will hit row
Boolean ischecked =((CheckBox)(row.FindControl("chkval"))).Checked ;
//will find checkbox which has selected or checked
if (ischecked)
{
txtnm.Text = ((Label)(row.FindControl("lblNm"))).Text;
delete(txtnm.Text);
}
else
{
}
bind();
}
now check for delete function from where we are passing value from checked event.
public void delete(string nm)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from stud where nm='"+nm+"'", con);
int i= cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
string deletevalue = "<script>alert(record deleted succesfully);</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", deletevalue,false);
}
else
{
}
}
now after click on checkbox value will be deleted from respective table and database.
<asp:GridView runat="server" ID="grd" AutoGenerateColumns ="false" >
<Columns >
<asp:TemplateField headertext ="Id">
<ItemTemplate >
<%#Eval("id")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Name">
<ItemTemplate >
<asp:Label runat="server" ID="lblNm" Text='<%#Eval("nm")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Select">
<ItemTemplate >
<%#Eval("lnm")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Select">
<ItemTemplate >
<asp:CheckBox ID="chkval" runat="server" AutoPostBack="true" OnCheckedChanged="chkval_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext ="Lname">
<ItemTemplate >
<input type =text id="txtAdd" value ='<%# "Mr " +Eval("nm")+" "+ Eval("lnm") %>'/></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void bind()
{
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from stud", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
sda.Fill(ds);
ViewState["viewds"] = ds;
grd.DataSource = ds;
grd.DataBind();
con.Close();
}
0 comments: