under.aspx
-----------
<asp:GridView DataKeyNames="sno" ID="GridView1"
runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="sno" HeaderText="sno" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("sno") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
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;
public partial class dummy : System.Web.UI.Page
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader dr = null;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
con.Open();
cmd = new SqlCommand("select sno,name from dummy",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record " +
DataBinder.Eval(e.Row.DataItem, "sno") + "')");
}
}
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
// get the categoryID of the clicked row
int sno = Convert.ToInt32(e.CommandArgument);
// Delete the record
// DeleteRecordByID(categoryID);
con.Open();
cmd = new SqlCommand("delete from dummy where sno=@sno", con);
cmd.Parameters.Add(new SqlParameter("@sno", sno));
cmd.ExecuteNonQuery();
con.Close();
// Implement this on your own :)
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int sno = (int)GridView1.DataKeys[e.RowIndex].Value;
// DeleteRecordByID(categoryID);
con.Open();
cmd = new SqlCommand("delete from dummy where sno=@sno",con);
cmd.Parameters.Add(new SqlParameter("@sno",sno));
cmd.ExecuteNonQuery();
con.Close();
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
}
}
No comments:
Post a Comment