Create a database name Office with Four Column
1.OfficeId(Auto Increment-Int)
2.OfficeName(nvarhcar(50))
3.City(nvarchar(50))
4.Country(nvarchar(50))
-> Create a .aspx Page name Office.aspx.
-> Create a UserControl OfficeAddEdit.ascx Page.
-> Create a UserControl OfficeList.ascx Page.
-> Add Images Folder and add Edit.gif Image.
Office.aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Office.aspx.cs" Inherits="OfficeDelegate.Office" %>
<%@ Register Src="~/OfficeList.ascx" TagName="OfficeList" TagPrefix="uc1" %>
<%@ Register Src="~/OfficeAddEdit.ascx" TagName="OfficeAddEdit" TagPrefix="uc2" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="RightTable">
<tr>
<td colspan="2" class="TableHeader">
<asp:Label ID="lblAvatarappearanceMasterHeader" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td>
<uc1:OfficeList ID="OfficeList1" runat="server" />
<uc2:OfficeAddEdit ID="OfficeAddEdit1" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Office.aspx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OfficeDelegate
{
public delegate void OfficeEditHandler(object sender, OfficeDelegate.OfficeList.OfficeMasterInfo args);
public delegate void OfficeAddHandler();
public delegate void OfficeHandler();
public partial class Office : System.Web.UI.Page
{
public int TabID
{
get
{
if (ViewState["TabId"] == null)
return 0;
else
return Convert.ToInt32(ViewState["TabId"]);
}
set
{
ViewState["TabId"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
OfficeList1.EditEvent += new OfficeEditHandler(List_eventhandler);
OfficeList1.AddEvent += new OfficeAddHandler(ShowDetails_eventhandler);
OfficeAddEdit1.eventhandler += new OfficeHandler(Detail_eventhandler);
if (!IsPostBack)
{
TabID = 1;
ShowHideControl();
}
}
void ShowDetails_eventhandler()
{
OfficeAddEdit1.OfficeId = 0;
TabID = 2;
ShowHideControl();
}
void Detail_eventhandler()
{
TabID = 1;
ShowHideControl();
}
void List_eventhandler(object sendar, OfficeDelegate.OfficeList.OfficeMasterInfo asgs)
{
OfficeAddEdit1.OfficeId = asgs.iOfficeId;
TabID = 2;
ShowHideControl();
}
private void ShowHideControl()
{
if (TabID == 1)
{
OfficeList1.Visible = true;
OfficeList1.View();
OfficeAddEdit1.Visible = false;
}
else if (TabID == 2)
{
OfficeAddEdit1.Visible = true;
OfficeAddEdit1.View();
OfficeList1.Visible = false;
}
}
}
}
Now OfficeList.ascx Page
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OfficeList.ascx.cs" Inherits="OfficeDelegate.OfficeList" %>
<asp:Label ID="lblList" runat="server" Text="List Page"></asp:Label>
<asp:GridView ID="gvList" runat="server" AutoGenerateColumns="false"
Width="100%" DataKeyNames="OfficeId" onrowcommand="gvList_RowCommand"
onrowediting="gvList_RowEditing" >
<Columns>
<asp:TemplateField ItemStyle-Width="10px">
<HeaderTemplate>
Select
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"/>
</ItemTemplate>
<ItemStyle Width="5%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="400px" HeaderText="OfficeName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("OfficeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="400px" HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="400px" HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/Images/Edit.gif" CommandName="Edit"
ToolTip='Edit' CommandArgument='<%# Eval("OfficeId") %>' />
</ItemTemplate>
<ItemStyle Width="5%" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<div align="right">
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" />
</div>
OfficeList.ascx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OfficeDelegate
{
public partial class OfficeList : System.Web.UI.UserControl
{
#region Public Properties
public event OfficeEditHandler EditEvent;
public event OfficeAddHandler AddEvent;
#endregion
public int TabID
{
get
{
if (ViewState["TabId"] == null)
return 0;
else
return Convert.ToInt32(ViewState["TabId"]);
}
set
{
ViewState["TabId"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
#region Methods
private void Bind()
{
try
{
OfficeDataContext db = new OfficeDataContext();
var office = from l in db.Offices select l;
gvList.DataSource = office;
gvList.DataBind();
}
catch (Exception ex)
{
}
}
#endregion
public void View()
{
Bind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddEvent();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
OfficeDataContext db = new OfficeDataContext();
for (int i = 0; i < gvList.Rows.Count; i++)
{
CheckBox chk = (CheckBox)(gvList.Rows[i].FindControl("chkSelect"));
if (chk.Checked == true)
{
var delete = from l in db.Offices where l.OfficeId == Convert.ToInt32(gvList.DataKeys[i].Value) select l;
db.Offices.DeleteAllOnSubmit(delete);
db.SubmitChanges();
}
}
Bind();
}
protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Edit")
{
int iID = Convert.ToInt32(e.CommandArgument.ToString());
OfficeMasterInfo args = new OfficeMasterInfo();
args.iOfficeId = iID;
EditEvent(this, args);
TabID = 2;
}
}
catch (Exception ex)
{
}
}
protected void gvList_RowEditing(object sender, GridViewEditEventArgs e)
{
}
public class OfficeMasterInfo
{
public int iOfficeId;
}
}
}
OfficeAddEdit.ascx Page
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OfficeAddEdit.ascx.cs" Inherits="OfficeDelegate.OfficeAddEdit" %>
<table>
<tr>
<td>
<asp:Label ID="lblName" Text="OfficeName" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblCity" Text="City" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCountry" Text="Country" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<div align="right">
<asp:Button ID="btnAdd" Text="Add" runat="server" onclick="btnAdd_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel"
onclick="btnCancel_Click"/>
</div>
OfficeAddEdit.ascx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OfficeDelegate
{
public partial class OfficeAddEdit : System.Web.UI.UserControl
{
public int TabID
{
get
{
if (ViewState["TabId"] == null)
return 0;
else
return Convert.ToInt32(ViewState["TabId"]);
}
set
{
ViewState["TabId"] = value;
}
}
public int OfficeId
{
get
{
if (ViewState["Id"] == null)
return 0;
else
return Convert.ToInt32(ViewState["Id"]);
}
set
{
ViewState["Id"] = value;
}
}
public event OfficeHandler eventhandler;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Insert();
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
TabID = 1;
eventhandler();
}
public void View()
{
if (OfficeId != 0)
{
SetTocotrol();
}
else
{
txtName.Text = string.Empty;
txtCity.Text = string.Empty;
txtCountry.Text = string.Empty;
}
}
private void SetTocotrol()
{
OfficeDataContext db = new OfficeDataContext();
Office office = (from l in db.Offices where l.OfficeId == OfficeId select l).FirstOrDefault();
txtName.Text = office.OfficeName;
txtCity.Text = office.City;
txtCountry.Text = office.Country;
}
public void Insert()
{
if (OfficeId == 0)
{
OfficeDataContext db = new OfficeDataContext();
var office = new Office { OfficeName = txtName.Text, City = txtCity.Text, Country = txtCountry.Text };
db.Offices.InsertOnSubmit(office);
db.SubmitChanges();
TabID = 1;
eventhandler();
}
else
{
OfficeDataContext db = new OfficeDataContext();
Office office = (from l in db.Offices where l.OfficeId == OfficeId select l).FirstOrDefault();
office.OfficeName = txtName.Text;
office.City = txtCity.Text;
office.Country = txtCountry.Text;
//db.Offices.InsertOnSubmit(office);
db.SubmitChanges();
TabID = 1;
eventhandler();
}
}
}
}
0 comments:
Post a Comment