Sunday, January 5, 2014

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 6 (Variables and Functions)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 6 (Variables and Functions)

PRE-REQUISITE

Or

STEPS

1) Open Web Site vbsite201
2) Edit Default.aspx
Add a property OnClick with value “Click”
Add a label Object Label5
Add a script for “Click”
3) Run.
1. Enter…
Name: John
DOB: 1/1/2000
Age Now: 14
2. Click Submit.
3. There is a possibility that the user calculates his age incorrectly. We need to assist the user to do this.
4) Modify the script.
1. Add a Function FindAge
2. Add function call to FindAge from within Sub Click
Take note that this server-side script may not be the most efficient way to compute user age.
This example is just to illustrate the use of variables and functions in ASP.NET Web Site.
5) Run.

C# EXERCISE

string FindAge(string strDate)
{
    DateTime dob = Convert.ToDateTime(strDate)
    textBox1.Text = dob.ToString();
    TimeSpan tm = (DateTime.Now - dob);
    int age = (tm.Days/365) ;  
    return age.ToString();
}
void Click(Object s, EventArgs e)
{
    Label5.Text = "You have entered...<br/>"
    Label5.Text += "Name: " + TextBox1.Text + "<br/>"
    Label5.Text += "DOB: " + TextBox2.Text + "<br/>"
    Label5.Text += "Age Now: " + FindAge(TextBox2.Text) + "<br/>"
}

FURTHER EXERCISE

How to add values e.g. “BMW”, “Toyota”, “Honda” to DropDown object during Page_Load event?
Clue: DropDownList.Items.Add(…)

DISCUSSION

Variables.
Variable Data Types.
Data Type Conversion.
Function.
Function Call.

DOWNLOAD

---

No comments:

Post a Comment