example of gridview modelpopup using asp.net with C#
Show Gridview Data
ViewRecord in popupmenu
Add New Record
Edit Record
Gridview.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="gridviewpopup.aspx.cs"
Inherits="gridviewpopup"
%>
<%@ 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></title>
<style type="text/css">
.modalBackground
{
background-color: Gray;
opacity: 0.50;
filter: alpha(opacity=80);
z-index: 10000;
}
.div1
{
Width:302px
;
height:19px;
background-color:Red
}
.div2
{
Width:244px
;
height:19px;
background-color:Blue
}
.div3
{
Width:196px
;
height:19px;
background-color:Blue
}
</style>
</head>
<body bgcolor="black">
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="Button1"
CancelControlID="ImageButton1" PopupControlID="Panel1" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<%----%>
<asp:Button ID="Button1" BackColor="White" runat="server" Text="show grid"
onclick="Button1_Click"/>
<%--<asp:Button
ID="Button2" BackColor="White" runat="server"
Text="update" onclick="Button2_Click" />--%>
<%--<asp:LinkButton
ID="LinkButton2" ForeColor="White" runat="server"
onclick="LinkButton2_Click">LinkButton</asp:LinkButton>--%>
<asp:Panel ID="Panel1" runat="server" BorderStyle="Solid" BorderColor="White" BackColor="BlueViolet" Visible="false">
<div class="div1" >
<asp:ImageButton ID="ImageButton1" Width="20px" Height="20px" style="position:absolute; left:280px; right:10px" runat="server" ImageUrl="~/image/delete.jpeg" />
</div>
<asp:GridView ID="GridView1" DataKeyNames="emp_id" BorderColor="White"
ForeColor="White" runat="server"
AutoGenerateColumns="false" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Views">
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" ForeColor="Green" OnClick="view" CommandArgument='<%#Eval("emp_id")
%>' runat="server">view</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClick=edit CommandArgument='<%#Eval("emp_id")
%>' runat="server">edit</asp:LinkButton>
<%--<asp:LinkButton
ID="LinkButton1" ForeColor="Yellow"
CommandName="edit" CommandArgument='<%#Eval("emp_id")
%>' runat="server">edit</asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DELETE">
<ItemTemplate>
<%--<asp:LinkButton
ID="LinkButton1" CommandArgument='<%#Eval("emp_id")
%>' runat="server">edit</asp:LinkButton>--%>
<asp:LinkButton ID="LinkButton2" ForeColor="Yellow" CommandName="delete" CommandArgument='<%#Eval("emp_id")
%>' runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_NAME">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("emp_nm")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_add">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("emp_add")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Panel Visible="false" ID="Panel3" BorderStyle="Solid" BorderColor="White"
runat="server"
BackColor="Red"
>
<div class="div3">
<asp:ImageButton ID="ImageButton3" Width="20px" Height="20px"
style="position:absolute; left:180px; right:10px" runat="server"
ImageUrl="~/image/delete.jpeg" onclick="ImageButton2_Click"
/>
</div>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="EMP_NAME">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("emp_nm")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_add">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#Eval("emp_add")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender4" runat="server"
TargetControlID="Button6" PopupControlID="Panel3" BackgroundCssClass="modalBackground" >
</asp:ModalPopupExtender>
<asp:Button ID="Button6" runat="server" Visible="false" Text="Button" onclick="Button6_Click" />
<asp:Button ID="Button2" BackColor="White" runat="server" Text="add" onclick="Button2_Click"
/>
<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
TargetControlID="Button2"
PopupControlID="Panel2"
BackgroundCssClass="modalBackground" >
</asp:ModalPopupExtender>
<asp:Panel ID="Panel2" BorderStyle="Solid" BorderColor="White" runat="server" BackColor="Red" >
<div class="div2">
<asp:ImageButton ID="ImageButton2" Width="20px" Height="20px"
style="position:absolute; left:225px; right:10px" runat="server"
ImageUrl="~/image/delete.jpeg" onclick="ImageButton2_Click"
/>
</div>
<table border="1" bordercolor="white">
<tr>
<td>
<asp:Label ID="Label3" runat="server"
Text="emp_name"
ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server"
Text="emp_address"
ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button3"
BackColor="White"
runat="server"
Text="insert"
onclick="Button3_Click1" />
<asp:Button ID="Button5"
runat="server"
Text="update"
Visible="false"
onclick="Button5_Click" />
</td>
</tr>
</table>
<asp:Button ID="Button4" runat="server" Text="Button" style="display:none"
onclick="Button4_Click" />
<asp:ModalPopupExtender ID="ModalPopupExtender3" runat="server"
TargetControlID="Button4" PopupControlID="Panel2" BackgroundCssClass="modalBackground" >
</asp:ModalPopupExtender>
</asp:Panel>
</asp:Panel>
</div>
</form>
</body>
</html>
gridview.aspx.cs
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.IO;
public partial class gridviewpopup : System.Web.UI.Page
{
DataClassesDataContext
dc = new DataClassesDataContext();
protected void Page_Load(object
sender, EventArgs e)
{
grid();
}
public void grid()
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
string
q = "select * from pi_emp_data";
DataSet
ds = new DataSet();
SqlDataAdapter
da = new SqlDataAdapter(q,
conn);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
protected void Button1_Click(object
sender, EventArgs e)
{
Panel1.Visible = true;
ModalPopupExtender1.Show();
grid();
}
protected void Button3_Click1(object
sender, EventArgs e)
{
pi_emp_data
dt = new pi_emp_data();
dt.emp_nm = TextBox1.Text;
dt.emp_add = TextBox2.Text;
dc.pi_emp_datas.InsertOnSubmit(dt);
dc.SubmitChanges();
Page_Load(null,
null);
TextBox1.Text = "";
TextBox2.Text = "";
Panel1.Visible = true;
ModalPopupExtender1.Show();
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
Session["b"]
= GridView1.DataKeys[e.RowIndex].Value.ToString();
var
myEmployee = dc.pi_emp_datas.Single(myemp => myemp.emp_id == int.Parse(Session["b"].ToString()));
dc.pi_emp_datas.DeleteOnSubmit(myEmployee);
try
{
dc.SubmitChanges();
Response.Write("Deleted");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Panel1.Visible = true;
ModalPopupExtender1.Show();
grid();
}
protected void edit(object
sender, EventArgs e)
{
Panel2.Visible = true;
string l = ((LinkButton)sender).CommandArgument;
foreach
(GridViewRow r in
GridView1.Rows)
{
Session["a"]
= GridView1.DataKeys[r.RowIndex].Value.ToString();
if
(l.Equals(Session["a"].ToString()))
{
Session["b"]
= GridView1.DataKeys[r.RowIndex].Value.ToString();
string s
= Session["b"].ToString();
Label
l1 = (Label)GridView1.Rows[r.RowIndex].FindControl("Label1");
TextBox1.Text = l1.Text;
Label
l2 = (Label)GridView1.Rows[r.RowIndex].FindControl("Label2");
TextBox2.Text = l2.Text;
}
}
Panel1.Visible = true;
ModalPopupExtender1.Show();
ModalPopupExtender2.Show();
Button3.Visible = false;
Button5.Visible = true;
}
protected void Button4_Click(object
sender, EventArgs e)
{
Panel2.Visible = true;
ModalPopupExtender2.Show();
grid();
}
protected void Button5_Click(object
sender, EventArgs e)
{
pi_emp_data
dt = new pi_emp_data();
var
myEmployee = dc.pi_emp_datas.Single(myemp => myemp.emp_id == int.Parse(Session["b"].ToString()));
string
s = Session["b"].ToString();
myEmployee.emp_nm = TextBox1.Text;
myEmployee.emp_add = TextBox2.Text;
try
{
dc.SubmitChanges();
Response.Write("Updated");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Panel2.Visible = true;
ModalPopupExtender1.Show();
grid();
TextBox1.Text = "";
TextBox2.Text = "";
Button5.Visible = false;
Button3.Visible = true;
}
protected void Button2_Click(object
sender, EventArgs e)
{
Button3.Visible = true;
Button5.Visible = false;
}
protected void ImageButton2_Click(object
sender, ImageClickEventArgs e)
{
Panel3.Visible = false;
//Panel1.Visible
= true;
//ModalPopupExtender1.Show();
//grid();
Panel1.Visible = true;
ModalPopupExtender1.Show();
Button3.Visible = true;
Button5.Visible = false;
TextBox1.Text = "";
TextBox2.Text = "";
}
protected void view(object
sender, EventArgs e)
{
//ModalPopupExtender4.Show();
string
l = ((LinkButton)sender).CommandArgument;
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
string
q = "select * from pi_emp_data where
emp_id='" + l + "'";
DataSet
ds = new DataSet();
SqlDataAdapter
da = new SqlDataAdapter(q,
conn);
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
conn.Close();
Panel1.Visible = true;
ModalPopupExtender1.Show();
Panel3.Visible = true;
ModalPopupExtender4.Show();
}
protected void Button6_Click(object
sender, EventArgs e)
{
}
}
0 comments :