Web Forms Codebehind Example |
|
This page builds on our exmple webform contact.aspx page we created in Step 1 ASP.Net Server Controls. When you
created contact.aspx using Visual Studio
or Visual Web Developer you should have a codebehind page called contact.aspx.cs.
|
Step 2. Web Form Codebehind
Your basic codebehind for contact.aspx.cs should look like the example below. We're going to use frmValidate to handle the form processing from the onclick event.
|
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void frmValidate(object Source, EventArgs e)
{
}
}
|
|
|
|
|
In the onclick subroutine for frmValidate we use a conditional statement to check the Page.IsPostBack
using Page.IsValid property indicating if page validation succeeded.
Next we create a local string variable to store the results we want to display back to the vistor so they feel good that we received their information correctly.
Keeping security best practices in mind
whenever dispalying data that has been submitted we use the Server.HtmlEncode method.
|
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void frmValidate(object Source, EventArgs e)
{
if (IsPostBack)
if (IsValid)
{
string confirmation;
confirmation = "<p><h3>THANK YOU. We have recieved
your information.</h3> <br>Please do not submit it again.<br><br>";
confirmation += "<p>Full Name: ";
confirmation += Server.HtmlEncode(FName.Text + " " + LName.Text);
confirmation += "<br>Address: " + Server.HtmlEncode(Street.Text);
confirmation += "<br>City: " + Server.HtmlEncode(City.Text);
confirmation += "<br>State: " + Server.HtmlEncode(State.Text);
confirmation += "<br>Zipcode: " + Server.HtmlEncode(Zip.Text);
confirmation += "<br>Email: " + Server.HtmlEncode(email.Text);
confirmation += "<br>Phone: " + Server.HtmlEncode(Telephone.Text);
confirmation += "<br>Contact Me:" + Server.HtmlEncode(Contactme.Text);
confirmation += "</p>";
statusLabel.Text = confirmation;
frmInfo.Visible = false;
}
else
statusLabel.Text = "Invalid Info Submitted";
}
}
|
|
|
As final cool trick we hide the webform using using the statement "frmInfo.Visible = false;" so all the visitor sees is the confirmation string we wrote back to the screen using the ASP:Label control we created in Step 1 ASP.Net Server Controls with the statetment: "statusLabel.Text = confirmation;"
|
Test Drive |
|
You can test the final product of our Step 2 contact.aspx form. For the purposes of our demonstration in this step we're going to just post the results back to the screen. In Step 3. Email results we're going to add a subroutine to email the form results to the webmaster and the visitor.
|