Crosspage Posting
Crosspage Posting
Crosspage Posting
Crosspage1.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Crosspage1.aspx.cs" Inherits="crosspage1" %> <!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 id="Head1" runat="server"> <title>Cross Page 1</title> </head> <body> <form id="form1" runat="server"> <div> <b>Cross Page 1 </b><br /><br /><br /> First Name <asp:TextBox ID="txtFN" runat="server"></asp:TextBox><br/> Last Name <asp:TextBox ID="txtLN" runat="server"></asp:TextBox><br/><br/> <asp:Button ID="cmdPost" Text="Post Back" runat="server" PostBackUrl="~/Crosspage2.aspx" /> </div> </form> </body> </html>
Crosspage1.aspx.cs using using using using using using using using using using using using System; System.Collections; System.Configuration; System.Data; System.Linq; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.HtmlControls; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Xml.Linq;
public partial class crosspage1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public TextBox FirstNameTxt { get { return txtFN; } } public TextBox LastNameTxt { get { return txtLN; } } public String FullName { get { return txtFN.Text + " " + txtLN.Text; } } }
Crosspage2.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Crosspage2.aspx.cs" Inherits="crosspage2" %> <!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 id="Head1" runat="server"> <title> Page 2</title> </head> <body> <b>Cross Page 2 </b><br /> <form id="form1" runat="server"> <div> <asp:Label ID="lblInfo" runat="server"></asp:Label> </div> </form> </body> </html> Crosspage2.aspx.cs using using using using using using using using using using using using System; System.Collections; System.Configuration; System.Data; System.Linq; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.HtmlControls; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Xml.Linq;
public partial class crosspage2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { crosspage1 pg = PreviousPage as crosspage1; if (PreviousPage != null) { lblInfo.Text = "You come from a page titled " + PreviousPage.Title + "<br/> <br/>You Typed this: " + pg.FullName; } } }
OUTPUT
Validation
Validation.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Validation.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title>Validation controls</title> </head> <body> <form id="form1" runat="server"> <div> Please fill out the following form<br /> <em> All field are required & must contain valid information </em> <br /> <br /> <table> <tr> <td style="width:100px" valign="top">Name:</td> <td style="width:100px" valign="top"> <asp:TextBox ID="nameTextBox" runat="server"> </asp:TextBox> <asp:RequiredFieldValidator ID="nameInputValidator" runat="server" ControlToValidate="nameTextBox" ErrorMessage="please enter u r name" Display="Dynamic"></asp:RequiredFieldValidator> </td> </tr> <tr> <td style="width:100px" valign="top"> Email address:</td> <td style="width:450px" valign="top"> <asp:TextBox ID="emailTextBox" runat="server"> </asp:TextBox> e.g.,user@domain.com<br /> <asp:RequiredFieldValidator ID="emailInputValidator" runat="server" ControlToValidate="emailTextBox" ErrorMessage="please enter u r email add:" Display="Dynamic"> </asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+ ([-.]\w+)*" ControlToValidate="emailTextBox"></asp:RegularExpressionValid ator> </td> </tr> <td style="width:100px" valign="top">Phone number:</td> <td style="width:450px" valign="top" id="phoneInputValidator"> <asp:TextBox ID="phoneTextBox" runat="server"> </asp:TextBox> eg.,(555) 555-1234<br /> </td> <asp:RequiredFieldValidator ID="PhoneInputValidator" runat="server" ControlToValidate="phoneTextBox" ErrorMessage="Please enter u r phone no." Display="Dynamic"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="PhoneFormatValidator" runat="server" ControlToValidate="phoneTextBox" Display="Dynamic" ErrorMessage="please neter a phone no in a valid form" ValidationExpression="((\(\d{3}\) ?)| (\d{3}-))?\d{3}-\d{4}"></asp:RegularExpressionValidator> </table> <br /> <asp:Button ID="submitButton" runat="server" Text="Submit" /> <br /><br /> <asp:Label ID="OutputLabel" runat="server" Text="Thanku you for u r submition" Visible="false"></asp:Label> </div> </form> </body> </html>
Validation.aspx using using using using using using using using using using using System; System.Configuration; System.Data; System.Linq; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.HtmlControls; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Xml.Linq;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { string name = Request.Form["nameTextBox"]; string email = Request.Form["emailTextBox"]; string phone = Request.Form["phoneTextBox"]; OutputLabel.Text += "<br/>We get following information:" + "<table style=\"background-color:yellow\">" + "<tr><td>Name:</td><td>" + name + "</td></tr>" + "<tr><td>Email add:</td><td>" + email + "</td></tr>" + "<tr><td>Phone number:</td><td>" + phone + "</td></tr>" + "<table>"; OutputLabel.Visible = true; } } }
OUTPUT
Currency Converter
Curr_Conv.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Curr_Conv.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title>Currency Converter</title> </head> <body> <form id="form1" runat="server"> <div> <p align="center"> <b>Currency Convertor</b> <br /> </p> Convert &n bsp; <input type="text" id="US" runat="server" /> <br /> U.S Dollars to <select id="currency" runat="server"> </select> <br/><br /> <input type="submit" value="OK" id="convert" onserverclick="Convert_Server_Click" runat="server" /><br /> <div style="font-weight:bold" id="result" runat="server"></div> </div> </form> </body> </html>
Curr_Conv.aspx.cs using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (this.IsPostBack == false) { currency.Items.Add(new ListItem("Euros", "0.85")); currency.Items.Add(new ListItem("Yen", "110.33")); currency.Items.Add(new ListItem("Canadian Dollar", "1.2")); } } protected void Convert_Server_Click(object sender, EventArgs e) { decimal amt = Decimal.Parse(US.Value); ListItem item = currency.Items[currency.SelectedIndex]; decimal newamt = amt * Decimal.Parse(item.Value); result.InnerText = amt.ToString() + " U.S. Dollars = "; result.InnerText += newamt.ToString() + " " + item.Text; }
OUTPUT
Custom Form
CustomForm.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomForm.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div style="height: 392px"> User Name: <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> <br /> <br /> Password &n bsp; <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br /><br /> Password Retype <asp:TextBox ID="txtRetype" runat="server" TextMode="Password"></asp:TextBox><br /><br /> Email &n bsp; <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br /><br /> Age: &n bsp; <asp:TextBox ID="txtAge" runat="server" Width="79px"></asp:TextBox><br /><br /> Reffered Code: <asp:TextBox ID="txtCode" runat="server"></asp:TextBox><br /><br /><br /> &n bsp; <asp:Button ID="cmdSubmit" runat="server" Text="Submit" OnClick="cmdSubmitClick" />
<asp:Button ID="cmdCancel" runat="server" Text="Cancel" CausesValidation="false" OnClick="cmdCancel_Click" /> </div> <asp:Label ID="lblMessage" runat="server"></asp:Label> <asp:RequiredFieldValidator ID="vldUserName" runat="server" ErrorMessage="You must enter a user name" ControlToValidate="txtUserName"> </asp:RequiredFieldValidator><br /> <asp:RequiredFieldValidator ID="vldPassword" runat="server" ErrorMessage="you must enter a password" ControlToValidate="txtPassword"></asp:RequiredFieldValidator><br /> <asp:CompareValidator ID="vldRetype" runat="server" ErrorMessage="Your password doesn't match" ControlToCompare="txtPassword" ControlToValidate="txtRetype"> </asp:CompareValidator> <br /> <asp:RequiredFieldValidator ID="vldRetypeRequired" runat="server" ErrorMessage="You must confirm the password" ControlToValidate="txtRetype"> </asp:RequiredFieldValidator> <br /> <asp:RegularExpressionValidator ID="vldEmail" runat="server" ErrorMessage="This email is missing the @ symbol" ValidationExpression=".+@.+" ControlToValidate="txtEmail"> </asp:RegularExpressionValidator> <br /> <asp:RangeValidator ID="vldAge" runat="server" ErrorMessage="this age is not btwn 1 and 120" Type="Currency" MinimumValue="1" MaximumValue="120" ControlToValidate="txtAge"> </asp:RangeValidator> <asp:CustomValidator ID="vldCode" runat="server" ErrorMessage="Try a string that with 014" ValidateEmptyText="false" OnServerValidate="vldCode_ServerValidate" ControlToValidate="txtCode"></asp:CustomValidator> </form> </body> </html>
CustomForm.aspx.cs
using using using using using using using using using using using System; System.Configuration; System.Data; System.Linq; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.HtmlControls; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Xml.Linq;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void cmdSubmitClick(object sender, EventArgs e) { lblMessage.Text = " "; if (Page.IsValid) { lblMessage.Text = "This is a valid form"; } } protected void cmdCancel_Click(object sender, EventArgs e) { lblMessage.Text = "No attempt was made to validate this form"; } protected void vldCode_ServerValidate(object source, ServerValidateEventArgs e) { try { int val = Int32.Parse(e.Value.Substring(0, 3)); if (val % 7 == 0) { e.IsValid = true; } else { e.IsValid = false; } } catch { e.IsValid = false; } } }
OUTPUT