Validate Email Address using RegularExpressionValidator

Sometimes there might be the requirement to Validate Email Address using RegularExpressionValidator in application.
If Email address Textbox on your form is Required Field, then try to use following code on your page.

<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="txtEmailRequired" runat="server" 
                            ControlToValidate="txtEmail" 
                            Text="* Required" 
                            Display="Dynamic" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                ErrorMessage="eg: emailAdd@domain.com" 
                                ControlToValidate="txtEmail"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                                Display="Dynamic">
</asp:RegularExpressionValidator>                     

If Email address Textbox on your form is NOT a Required Field, then you can use following code on your page.

<asp:TextBox ID="txtEmail" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                ErrorMessage="eg: emailAdd@domain.com" 
                                ControlToValidate="txtEmail"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                                Display="Dynamic">
</asp:RegularExpressionValidator>

Validate Password and Confirm Password using CompareValidator

When you want to compare Password and Confirm Password Textbox value, you can use CompareValidator using below code.

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredPass" runat="server" ControlToValidate="txtPassword" Text="*" />

Confirm Password <asp:TextBox ID="txtPasswordCompare" runat="server" TextMode="Password" />
<asp:CompareValidator runat="server" ID="ComparePass" ControlToValidate="txtPassword" ControlToCompare="txtPasswordCompare" Text="Password mismatch" />

Remove Querystring item in ASP.NET

If we try to remove/delete a query string directly using below code, we will get an error - collection is read-only.

Request.QueryString.Remove("QSname")

In order to solve above error problem, we need to write below code before we remove them.

// reflect to readonly property
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);

// remove
this.Request.QueryString.Remove("QSname");

Generate Random Number in C#

You can use below code to generate a new Random number each time.

Random rd = new Random();
Int32 rdNum = rd.Next();
Response.Write(rdNum.ToString());

Page life Cycle of an ASP.NET page

Following are the events occur during ASP.NET Page Life Cycle:

1)Page_PreInit
2)Page_Init
3)Page_InitComplete
4)Page_PreLoad
5)Page_Load
6)Control Events
7)Page_LoadComplete
8)Page_PreRender
9)SaveViewState
10)Page_Render
11)Page_Unload

Among above events Page_Render is the only event which is raised by page. So we can't write code for this event.