[Pluralsight] One ASP.NET From Scratch

Part 1: Web Forms

1. Introduction

  • Use HTML Controls
    • Use as server-side controls by adding runat = server.
    • They tend to make an easier transition from HTML and smaller pages
    • Two events supported
      • OnServerClick
      • OnServerChange
  • Use Web Controls
    • More consistent object interface
    • Richer controls composed of HTML controls

2. Demo code



In Code Behind:

protected void BttSubmit_Click(object sender, EventArgs e)
            LblMessage.Text = TxtInput.Text;

3. Posting data, page navigation

In order to retrieve the data from other page, we need to preserve that piece of data in session state.


protected void BttOk_Click(object sender, EventArgs e)
            var order = new ComputerOrder
                OrderId = 101,
                OrderName = TxtOrderName.Text,
                Customer = TxtCustomerName.Text,
                CustomerEmail = TxtCustomerEmail.Text,
                DeliveryDate = CldDeliveryDate.SelectedDate,
                PartNumber = int.Parse(TxtPartNumber.Text),
                Rush = RadioYes.Checked
            Session["currentOrder"] = order;

In redired page:

protected void Page_Load(object sender, EventArgs e)
            var order = Session["currentOrder"] as ComputerOrder;
            LblOrderName.Text = order.OrderName;
            LblCustomerName.Text = order.Customer;
            LblCustomerEmail.Text = order.CustomerEmail;
            LblDeliveryDate.Text = order.DeliveryDate.ToShortDateString();
            LblOrderNumber.Text = order.OrderId.ToString();
            LblPartNumber.Text = order.PartNumber.ToString();
            LblRushOrder.Text = order.Rush ? "Yes" : "No";

Part 2: Advanced Web Forms

State Management

  • Session State
    • Same browser, data maintained server side
  • Application State
    • Multiple user on same web server
    • Maintained server side
  • Cookies
    • Same browser, maintained client side
  • ViewState
    • Maintained client site
    • Storing data in viewstate
    • Classes must be marked serializable
    • Must cast on the way out

2. Session State vs Application State

If we save data in Session State, we can retrieve it later if we are still using the same particular browser.

If we save data in Application State, we can retrieve it later even when we use different browser since the data is tied to the entire application.


Session["Email"] = "tu@gmail.com";
Application["Name"] = "Tu";

3. Cookies

var cookie = new HttpCookie("name", value);

4. Validation

Validation Controls

  • RequiredFieldValidator
  • CompareValidator
  • RangeValidator
  • RegularexpressionValidator
  • CustomValidator
  • DynamicValidator
  • ValidationSummary


In Code behind:

if (Page.IsValid)
                Message.Text = TextBox1.Text;

In Global.asax:

protected void Application_Start(object sender, EventArgs e)
            ValidationSettings.UnobtrusiveValidationMode = UnobtrusiveValidationMode.WebForms;

Install package: jquery and Aspnet.ScriptManager.Jquery

5. Data Sources

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dienchanonlineConnectionString %>" SelectCommand="SELECT [FirstName], [LastName], [Email] FROM [Customers]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s