In this post, I explain you about how to edit the GridView Row Values in ASP.NET with Ajax. First, create a new project on Visual Studio. In ASP.NET write the following code:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> How to Edit the GridView Row Values in ASP.NET with Ajax </title>
<style type="text/css">
.modalBackground
{
background-color: black;
opacity: 0.7;
}
#grd
{
margin:25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:DropDownList></asp:DropDownList>
<h4>Details</h4>
<div>
<asp:GridView ID="grd" runat ="server" GridLines="Both" BorderColor="#4F81BD" BorderStyle="Solid"
BorderWidth="1px" Style="position: static" AutoGenerateColumns="false" > <Columns>
<asp:BoundField DataField="User_id" HeaderText="User ID" SortExpression="User_id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Email_Address" HeaderText="Email Address" SortExpression="Email_Address" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile No" SortExpression="Mobile" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkBtn" runat="server" OnClick="lnkbtn_Click">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BorderColor="Red" BorderWidth="2px"></RowStyle>
</asp:GridView>
</div>
<asp:Button ID="btnShow" Visible="true" runat="server" />
<ajax:ModalPopupExtender ID="ModalPopup" runat="server" TargetControlID="btnShow"
BackgroundCssClass="modalBackground" PopupControlID="PnlShow">
</ajax:ModalPopupExtender>
<div ID="PnlShow" runat="server" style="display: none;background-color:white;">
<span style="float: right; padding-right: 0px; margin: 0px;">
<asp:Button ID="btnClose" Text="X" runat ="server" />
</span>
<div style="margin:25px;">
<table >
<tr>
<td colspan="2" >User Details</td>
</tr>
<tr><td>User Id</td>
<td>Name</td>
<td>Email_Address</td>
<td>Mobile No.</td>
</tr>
<tr>
<td><asp:Label ID="lblID" runat="server" /></td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td><asp:TextBox ID="txtEmail" runat="server" /></td>
<td><asp:TextBox ID="txtMobile" runat="server" /></td>
</tr>
<tr>
<td>
<asp:Button ID="btnUpdate" CommandName="Update" runat="server" Text="Update" onclick="btnUpdate_Click"/>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
In C#.NET
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;
public partial class AjaxModalPopUp : System.Web.UI.Page
{
SP obj = new SP();
protected void Page_Load(object sender, EventArgs e)
{
BindData();
btnShow.Visible = false;
}
protected void BindData()
{
DataSet ds = obj.Display();
grd.DataSource = ds;
grd.DataBind();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
obj.id = Convert.ToInt32(lblID.Text);
obj.name = txtName.Text;
obj.email = txtEmail.Text;
obj.mobile = txtMobile.Text;
obj.Update();
Response.Write ("<script>alert('Details Updated Successfully');</script>");
BindData();
}
protected void lnkbtn_Click(object sender, EventArgs e)
{
LinkButton LNK = sender as LinkButton;
GridViewRow gvrow = (GridViewRow)LNK.NamingContainer;
lblID.Text = gvrow.Cells[0].Text;
txtName.Text = gvrow.Cells[1].Text;
txtEmail.Text = gvrow.Cells[2].Text;
txtMobile.Text = gvrow.Cells[3].Text;
btnShow.Visible = true;
ModalPopup.Show();
}
}
In App_Code/SP.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Configuration;
using System.Data.Common ;
public class SP
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string mobile { get; set; }
public int studentId { get; set; }
public string item { get; set; }
public DataSet Display()
{
DataSet ds;
try
{
DbCommand cmd = sqlCon.GetStoredProcCommand("Display_Users");
ds = sqlCon.ExecuteDataSet(cmd);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
public int Update()
{
int checkUser;
try
{
DbCommand cmd = sqlCon.GetStoredProcCommand("Update_user");
sqlCon.AddInParameter(cmd, "@userId", DbType.String, id);
sqlCon.AddInParameter(cmd, "@name", DbType.String, name);
sqlCon.AddInParameter(cmd, "@email", DbType.String, email);
sqlCon.AddInParameter(cmd, "@mobile", DbType.String, mobile);
checkUser = sqlCon.ExecuteNonQuery(cmd);
return checkUser;
}
catch (Exception ex)
{
throw ex;
}
}
}
In VB.net
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class AjaxModalPopUp
Inherits System.Web.UI.Page
Private obj As New SP()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
BindData()
btnShow.Visible = False
End Sub
Protected Sub BindData()
Dim ds As DataSet = obj.Display()
grd.DataSource = ds
grd.DataBind()
End Sub
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)
obj.id = Convert.ToInt32(lblID.Text)
obj.name = txtName.Text
obj.email = txtEmail.Text
obj.mobile = txtMobile.Text
obj.Update()
Response.Write("<script>alert('Details Updated Successfully');</script>")
BindData()
End Sub
Protected Sub lnkbtn_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim LNK As LinkButton = TryCast(sender, LinkButton)
Dim gvrow As GridViewRow = DirectCast(LNK.NamingContainer, GridViewRow)
lblID.Text = gvrow.Cells(0).Text
txtName.Text = gvrow.Cells(1).Text
txtEmail.Text = gvrow.Cells(2).Text
txtMobile.Text = gvrow.Cells(3).Text
btnShow.Visible = True
ModalPopup.Show()
End Sub
End Class
In App_Code/SP.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.Configuration
Imports System.Data.Common
Public Class SP
Public Property id() As Integer
Get
Return m_id
End Get
Set(ByVal value As Integer)
m_id = Value
End Set
End Property
Private m_id As Integer
Public Property name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = Value
End Set
End Property
Private m_name As String
Public Property email() As String
Get
Return m_email
End Get
Set(ByVal value As String)
m_email = Value
End Set
End Property
Private m_email As String
Public Property mobile() As String
Get
Return m_mobile
End Get
Set(ByVal value As String)
m_mobile = Value
End Set
End Property
Private m_mobile As String
Public Property studentId() As Integer
Get
Return m_studentId
End Get
Set(ByVal value As Integer)
m_studentId = Value
End Set
End Property
Private m_studentId As Integer
Public Property item() As String
Get
Return m_item
End Get
Set(ByVal value As String)
m_item = Value
End Set
End Property
Private m_item As String
Public Function Display() As DataSet
Dim ds As DataSet
Try
Dim cmd As DbCommand = sqlCon.GetStoredProcCommand("Display_Users")
ds = sqlCon.ExecuteDataSet(cmd)
Return ds
Catch ex As Exception
Throw ex
End Try
End Function
Public Function Update() As Integer
Dim checkUser As Integer
Try
Dim cmd As DbCommand = sqlCon.GetStoredProcCommand("Update_user") sqlCon.AddInParameter(cmd, "@userId", DbType.[String], id)
sqlCon.AddInParameter(cmd, "@name", DbType.[String], name)
sqlCon.AddInParameter(cmd, "@email", DbType.[String], email)
sqlCon.AddInParameter(cmd, "@mobile", DbType.[String], mobile)
checkUser = sqlCon.ExecuteNonQuery(cmd)
Return checkUser
Catch ex As Exception
Throw ex
End Try
End Function
End Class
HostForLIFE.eu Ajax Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.