Hi chetan,
Please refer below sample.
HTML
<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table cellpadding="2" cellspacing="0" border="1">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Status
</th>
<th>
Edit
</th>
</tr>
</thead>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
</td>
<td>
<asp:Button ID="btnEdit" runat="server" Text='Edit' OnClick="UpdateStatus" />
</td>
</ItemTemplate>
</asp:ListView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindList();
}
}
private void BindList()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblStatusDetails", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.ListView1.DataSource = dt;
this.ListView1.DataBind();
}
}
}
}
protected void UpdateStatus(object sender, EventArgs e)
{
Label id = (Label)((Button)sender).NamingContainer.FindControl("lblId");
Label status = (Label)((Button)sender).NamingContainer.FindControl("lblStatus");
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE tblStatusDetails SET [Status] =@Status WHERE ID=@ID", con))
{
cmd.Parameters.AddWithValue("@ID", id.Text);
cmd.Parameters.AddWithValue("@Status", "Aborted");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindList();
}
Screenshot