Web Coding
1. Page Structure Overview
#region properties
// private const variable
// private variable
// property
#endregion
#region method
#endregion
#region event handler
#endregion
#region utility/commom //canbe here or utility class
e.g:
ResetInput() by div
ErrorHandler()
OperationResultMessageHandler()
#endregion
2. variable vs property vs method
target: avoid multiple times call database
When every operations(data) can be completed in one request but different method: property + variable
private Customer _cust;
private Customer Cust
{
get
{
if(_cust==null)
{
_cust=calldb();
}
return _cust;
}
}
private void SampleMethod()
{
var c=Cust;
...
}
private void SampleMethodB()
{
var c=Cust;
...
}
When every operations can be completed in one request and same method: property + local variable(in method)
private Customer Cust
{
get
{
return calldb();
}
}
private void SampleMethod()
{
var c=Cust;
...
}
When every operations need to completed via multiple request: property + viewstate or session
private Customer Cust
{
get
{
if(Viewstate["cust"]==null)
{
Viewstate["cust"]=calldb();
}
return Viewstate["cust"];
}
}
private void SampleMethod()
{
var c=Cust;
...
}
When operations need parameters: method
3. Best Practice
3.1 use "?" for object
public int? SelectedItemValue;
3.2 use foreach
foreach(Customer c in customerList)
{
}
3.3 use using
using(SqlConnection conn =new SqlConnection())
{
}
3.4 Null is different with 0 or ""
3.5 Strategy Design Pattern
Base Class + same workflow sub classes, base class control work flow + common method
Note: use master page is sub pages have same layout
3.6 User control + Event handler
delegate all user control 's event to parent page event handler to handle,user control is only define the schema to display
Sample:forward user control's "Button1_Click" event to Default (Parent) page
1> UserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.ascx.cs"
Inherits="UsercontrolDelegateEventToParent.HelloWorld" %>
<div>
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
<p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></p>
</div>
2> UserControl.ascx.cs
public partial class HelloWorld : System.Web.UI.UserControl
{
#region controls
public Label WelcomeLabel
{
get
{
return Label1;
}
set
{
Label1 = value;
}
}
#endregion
#region event handler
public delegate void UserControlButton1_Click(object sender, EventArgs e);
protected event UserControlButton1_Click _btn1Click;
public event UserControlButton1_Click Btn1Click
{
add
{
if (this._btn1Click == null)
this._btn1Click += value;
}
remove
{
if (this._btn1Click != null)
this._btn1Click -= value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this._btn1Click != null)
this._btn1Click(sender, e);
}
3. Default.aspx (Parent Page)
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="UsercontrolDelegateEventToParent._Default" %>
<%@ Register Src="~/HelloWorld.ascx" TagName="UserControl" TagPrefix="HW" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<HW:UserControl id="uc1"
runat="server"
OnBtn1Click="Button1_Click"
></HW:UserControl>
</asp:Content>
4> Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
uc1.WelcomeLabel.Text = "Hello World!";
}
}
3.7 Give each enum item with a specific valueinstead of making it auto increasing, if some one add one item in between, then the number will screwed, especially you have been refereed by database record
public enum CarOptions
{
SunRoof = 0x01,
Spoiler = 0x02,
FogLights = 0x04,
TintedWindows = 0x08,
}
3.8 Use partial class in need
e.g: the entity framework generate class, if u want to add some properties for your business need, you can use business class
public partial class MyEntity {
public String MyCustomProperty {
get;
set;
}
}
4 make User Control as a atomic unit, should never open access to controls which is inside User Control