A sample of simple web n-tire application, which can be used in small web application. And can be enhanced to use in complex web applications.
- Presentation Layer
- Business Layer
- Data Access Layer
Business & Data Access Class Diagram:
Application output screen:
Code:
Default.aspx.cs
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
DataSet ds = Customer.GetCustomers();
MyDataGrid.DataSource = ds;
MyDataGrid.DataBind();
}
protected void ButSave_Click(object sender, EventArgs e)
{
try
{
Customer newCustomer = new Customer();
newCustomer.Name = TextName.Text;
newCustomer.Mobile = TextMobile.Text;
newCustomer.City = TextCity.Text;
newCustomer.Address = TextAddress.Text;
newCustomer.DOB = Convert.ToDateTime(TextDOB.Text);
newCustomer.SaveNew();
BindGrid();
}
catch (Exception ex)
{
LblErrMess.Text = ex.Message;
}
}
protected void ButShowGrid_Click(object sender, EventArgs e)
{
if (MyDataGrid.Visible)
{
ButShowGrid.Text = "Show Grid";
MyDataGrid.Visible = false;
}
else
{
ButShowGrid.Text = "Hide Grid";
MyDataGrid.Visible = true;
}
}
protected void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#C2D5FC'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataGridItem item in MyDataGrid.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
if (((CheckBox)item.Cells[6].FindControl("cbRows")).Checked)
{
Customer.DeleteMe(Convert.ToInt32(item.Cells[0].Text));
}
}
}
BindGrid();
}
protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void MyDataGrid_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "Update")
{
TextCustomerId.Text = e.Item.Cells[0].Text;
TextName.Text = e.Item.Cells[1].Text;
TextCity.Text = e.Item.Cells[2].Text;
TextMobile.Text = e.Item.Cells[3].Text;
TextAddress.Text = e.Item.Cells[4].Text;
TextDOB.Text = e.Item.Cells[5].Text;
ButSave.Enabled = false;
TextName.Focus();
}
}
protected void ButUpdate_Click(object sender, EventArgs e)
{
Customer Cust = new Customer();
Cust.CustomerId = Convert.ToInt32(TextCustomerId.Text);
Cust.Mobile = TextMobile.Text;
Cust.City = TextCity.Text;
Cust.Name = TextName.Text;
Cust.Address = TextAddress.Text;
Cust.DOB = Convert.ToDateTime(TextDOB.Text);
Cust.UpdateMe();
ButSave.Enabled = true;
ButUpdate.Enabled = false;
TextCustomerId.Text = "";
TextName.Text = "";
TextCity.Text = "";
TextMobile.Text = "";
TextAddress.Text = "";
TextDOB.Text = "";
BindGrid();
}
}
}
Default.aspx:
<script type="text/javascript" language="javascript">
function DeleteConfirmation()
{
if (confirm("Are you sure, you want to delete selected records ?")==true)
return true;
else
return false;
}
function CheckAll(chk)
{
all = document.getElementsByTagName("input");
for(i=0;i<all.length;i++)
{
if(all[i].type=="checkbox" && all[i].id.indexOf("MyDataGrid_ct") > -1)
{
all[i].checked = chk.checked;
}
}
}
</script>
<asp:DataGrid ID="MyDataGrid" AutoGenerateColumns="false" runat="server"
AllowPaging="true" PagerStyle-Mode="NextPrev" al
onitemdatabound="MyDataGrid_ItemDataBound" ShowFooter="true"
onpageindexchanged="MyDataGrid_PageIndexChanged" PageSize="6" onitemcommand="MyDataGrid_ItemCommand">
<Columns>
<asp:BoundColumn HeaderText="CustomerId" DataField="CustomerId"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Name" DataField="Name"></asp:BoundColumn>
<asp:BoundColumn HeaderText="City" DataField="City"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Mobile" DataField="Mobile"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Address" DataField="Address"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Date Of Birth" DataFormatString="{0:MM/dd/yyyy}" DataField="DOB"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="cbRows" runat="server"/>
</ItemTemplate>
<HeaderTemplate>
<input type="checkbox" id="mainCB" onclick="javascript:CheckAll(this);" />
</HeaderTemplate>
<FooterTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="Button1_Click" OnClientClick="return DeleteConfirmation();"/> </FooterTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Update" Text="Edit">
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>