Saturday, January 4, 2014

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 2 (Page_Load event)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 2 (Page_Load event)

PRE-REQUISITE

Or
Download startup file:

STEPS

1) Open the Web Site vbsite101.
Open Default.aspx.
2) Turn on the line number display (follow the steps here, http://visualstudio-steps.blogspot.com/2014/01/how-to-show-line-numbers-in-code-editor.html)
Refer the screenshot below. We will be editing the following elements:
<Script> (line no. 5)
<title> (line no.11)
<div> (line no.15)
3) We start with the easiest part, i.e. <title>:
<title>Welcome</title>
4) Next, edit the <div>:
    <div>
      <p>Hello there!</p>
      <p>
        The time is now:
        <%-- Display the current date and time --%>
      </p>
      <p>
        <%-- Declare the title as string and set it --%>
        <% Dim Info As String = "This is generated by a code render block."%>
        <%= Info %>
      </p>    
    </div>
5) Run.
6) We are done with a simple page displaying a value stored in a string variable. We will add action to respond to an event, i.e. page load. Close Internet Explorer first.
7) Switch to Design Mode.
Click the Toolbox sidebar.
Click the pin icon so that Toolbox is visible all the time.
8) Add a Label object to the form.
Click the Label icon in the Standard Group of the Toolbox and drag the icon to a location besides “The time is now:”.
9) Set the Label object properties.
In the Properties window, set…
ID: myTimeLabel
Text: <blank>
10) Switch to Source mode.
We have successfully added an object using GUI approach (we can also type the codes manually).
If you run the codes now, you won’t see the Label object as it doesn’t contain anything.
We will add an action that will put some texts into the label when the page load event takes place.
11) Add codes to the <script> element.
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    'set the label text to the current time
    myTimeLabel.Text = DateTime.Now.ToString()
  End Sub
12) Run.

C# EXERCISE:

Repeat the above steps to create the web site using C# language.
At Step 11, use the codes below (for C# language):
  protected void Page_Load(object sender, EventArgs e)
  {
    //set the label text to the current time
    myTimeLabel.Text = DateTime.Now.ToString();
  }

DISCUSSION

An ASP.NET page consists of the following elements:
 directives
 code declaration blocks
 code render blocks
 ASP.NET server controls
 server-side comments
 literal text and HTML tags
DOWNLOAD
---

No comments:

Post a Comment