Step 1. Create flowing table name "Stores"
CREATE TABLE [dbo].[stores](
[stor_id] [char](4) NOT NULL,
[stor_name] [varchar](40) NULL,
[stor_address] [varchar](40) NULL,
[city] [varchar](20) NULL,
[state] [char](2) NULL,
[zip] [char](5) NULL,
CONSTRAINT [UPK_storeid] PRIMARY KEY CLUSTERED
(
[stor_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
step 2. Right click on on your Solution Explorer and select "Manage Nue get Packge.." and install AjaxControlToolKit.dll file
Step 3. Copy and past the below ASPX page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Popup Window</title>
<style type="text/css">
.tableBackground {
background-color: silver;
opacity: 0.7;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div align="center">
<asp:GridView runat="server" ID="gridView1" DataKeyNames="stor_id" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="stor_name" HeaderText="Store Name" />
<asp:BoundField DataField="stor_address" HeaderText="Store Address" />
<asp:BoundField DataField="city" HeaderText="City" />
<asp:BoundField DataField="state" HeaderText="State" />
<asp:BoundField DataField="zip" HeaderText="Zip" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" Text="Edit" OnClick="lnkEdit_Click" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblmsg" runat="server" />
<asp:Button ID="modelPopup" runat="server" Style="display: none" />
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="modelPopup" PopupControlID="updatePanel"
CancelControlID="btnCancel" BackgroundCssClass="tableBackground">
</asp:ModalPopupExtender>
<asp:Panel ID="updatePanel" runat="server" BackColor="White" Height="230px" Width="300px" Style="display: none">
<table width="100%" cellspacing="4">
<tr style="background-color: #33CC66">
<td colspan="2" align="center">Store Details</td>
</tr>
<tr>
<td align="right" style="width: 45%">Stor ID:
</td>
<td>
<asp:Label ID="lblstor_id" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Store Name:
</td>
<td>
<asp:TextBox ID="txtstor_name" runat="server" />
</td>
</tr>
<tr>
<td align="right">Store Address:
</td>
<td>
<asp:TextBox ID="txtstor_address" runat="server" />
</td>
</tr>
<tr>
<td align="right">City:
</td>
<td>
<asp:TextBox ID="txtcity" runat="server" />
</td>
</tr>
<tr>
<td align="right">State:
</td>
<td>
<asp:TextBox ID="txtstate" runat="server" />
</td>
</tr>
<tr>
<td align="right">Zip:
</td>
<td>
<asp:TextBox ID="txtzip" runat="server" />
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="btnUpdate" CommandName="Update" runat="server" Text="Update Data" OnClick="btnModity_Click" />
</td>
<td>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</td>
</tr>
</table>
</asp:Panel>
</div>
</form>
</body>
</html>
Step 4. Now write here your Source code
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
public partial class _Default : System.Web.UI.Page
{
private SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=pubs;User ID=sa;Password=123");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadStores();
}
}
protected void loadStores()
{
cnn.Open();
SqlCommand cmd = new SqlCommand("Select * from stores", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int count = ds.Tables[0].Rows.Count;
cnn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gridView1.DataSource = ds;
gridView1.DataBind();
}
else
{
lblmsg.Text = " No data found !!!";
}
}
protected void btnModity_Click(object sender, EventArgs e)
{
string stor_id = lblstor_id.Text;
cnn.Open();
SqlCommand cmd = new SqlCommand("update stores set stor_name='" + txtstor_name.Text + "', stor_address='" + txtstor_address.Text + "', city='" + txtcity.Text + "', state='" + txtstate.Text + "', zip='" + txtzip.Text + "' where stor_id=" + lblstor_id.Text, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
lblmsg.Text = "Data Updated...";
lblmsg.ForeColor = System.Drawing.Color.Green;
loadStores();
}
protected void lnkEdit_Click(object sender, EventArgs e)
{
LinkButton btnsubmit = sender as LinkButton;
GridViewRow gRow = (GridViewRow)btnsubmit.NamingContainer;
lblstor_id.Text = gridView1.DataKeys[gRow.RowIndex].Value.ToString();
txtstor_name.Text = gRow.Cells[0].Text;
txtstor_address.Text = gRow.Cells[1].Text;
txtcity.Text = gRow.Cells[2].Text;
txtstate.Text = gRow.Cells[3].Text;
txtzip.Text = gRow.Cells[4].Text;
this.ModalPopupExtender1.Show();
}
}
CREATE TABLE [dbo].[stores](
[stor_id] [char](4) NOT NULL,
[stor_name] [varchar](40) NULL,
[stor_address] [varchar](40) NULL,
[city] [varchar](20) NULL,
[state] [char](2) NULL,
[zip] [char](5) NULL,
CONSTRAINT [UPK_storeid] PRIMARY KEY CLUSTERED
(
[stor_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
step 2. Right click on on your Solution Explorer and select "Manage Nue get Packge.." and install AjaxControlToolKit.dll file
Step 3. Copy and past the below ASPX page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Popup Window</title>
<style type="text/css">
.tableBackground {
background-color: silver;
opacity: 0.7;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div align="center">
<asp:GridView runat="server" ID="gridView1" DataKeyNames="stor_id" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="stor_name" HeaderText="Store Name" />
<asp:BoundField DataField="stor_address" HeaderText="Store Address" />
<asp:BoundField DataField="city" HeaderText="City" />
<asp:BoundField DataField="state" HeaderText="State" />
<asp:BoundField DataField="zip" HeaderText="Zip" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" Text="Edit" OnClick="lnkEdit_Click" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblmsg" runat="server" />
<asp:Button ID="modelPopup" runat="server" Style="display: none" />
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="modelPopup" PopupControlID="updatePanel"
CancelControlID="btnCancel" BackgroundCssClass="tableBackground">
</asp:ModalPopupExtender>
<asp:Panel ID="updatePanel" runat="server" BackColor="White" Height="230px" Width="300px" Style="display: none">
<table width="100%" cellspacing="4">
<tr style="background-color: #33CC66">
<td colspan="2" align="center">Store Details</td>
</tr>
<tr>
<td align="right" style="width: 45%">Stor ID:
</td>
<td>
<asp:Label ID="lblstor_id" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Store Name:
</td>
<td>
<asp:TextBox ID="txtstor_name" runat="server" />
</td>
</tr>
<tr>
<td align="right">Store Address:
</td>
<td>
<asp:TextBox ID="txtstor_address" runat="server" />
</td>
</tr>
<tr>
<td align="right">City:
</td>
<td>
<asp:TextBox ID="txtcity" runat="server" />
</td>
</tr>
<tr>
<td align="right">State:
</td>
<td>
<asp:TextBox ID="txtstate" runat="server" />
</td>
</tr>
<tr>
<td align="right">Zip:
</td>
<td>
<asp:TextBox ID="txtzip" runat="server" />
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="btnUpdate" CommandName="Update" runat="server" Text="Update Data" OnClick="btnModity_Click" />
</td>
<td>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</td>
</tr>
</table>
</asp:Panel>
</div>
</form>
</body>
</html>
Step 4. Now write here your Source code
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
public partial class _Default : System.Web.UI.Page
{
private SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=pubs;User ID=sa;Password=123");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadStores();
}
}
protected void loadStores()
{
cnn.Open();
SqlCommand cmd = new SqlCommand("Select * from stores", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int count = ds.Tables[0].Rows.Count;
cnn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gridView1.DataSource = ds;
gridView1.DataBind();
}
else
{
lblmsg.Text = " No data found !!!";
}
}
protected void btnModity_Click(object sender, EventArgs e)
{
string stor_id = lblstor_id.Text;
cnn.Open();
SqlCommand cmd = new SqlCommand("update stores set stor_name='" + txtstor_name.Text + "', stor_address='" + txtstor_address.Text + "', city='" + txtcity.Text + "', state='" + txtstate.Text + "', zip='" + txtzip.Text + "' where stor_id=" + lblstor_id.Text, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
lblmsg.Text = "Data Updated...";
lblmsg.ForeColor = System.Drawing.Color.Green;
loadStores();
}
protected void lnkEdit_Click(object sender, EventArgs e)
{
LinkButton btnsubmit = sender as LinkButton;
GridViewRow gRow = (GridViewRow)btnsubmit.NamingContainer;
lblstor_id.Text = gridView1.DataKeys[gRow.RowIndex].Value.ToString();
txtstor_name.Text = gRow.Cells[0].Text;
txtstor_address.Text = gRow.Cells[1].Text;
txtcity.Text = gRow.Cells[2].Text;
txtstate.Text = gRow.Cells[3].Text;
txtzip.Text = gRow.Cells[4].Text;
this.ModalPopupExtender1.Show();
}
}
now you can get your output like
No comments:
Post a Comment