Using ASP.NET Standard Controls: Label, Textbox, Button.

This topic will guide you how to use basic controls in asp.net application: label, textbox and button control. You can use Visual Studio or Visual Web Developer, open Toolbox from View menu if it is invisible. Drag the label control, text control, and text control into your web form, as below figure:

Label 1:
ID: Label1
Text: "What kind of music do you like?"

Label 2:
ID: Label2
Text: Empty. Note that label 2 will be invisible if button is not clicked.

Button:
ID: Button1
Text: Submit

Text Box:
ID: TextBox1
Text: Empty

We can run the web form by right click any place on form and choose "View in Browser". We'll see only Label 1, Text Box and Button, Label 2 is still invisible.

Now we write some code to display text when Button is clicked. Choose Button, double click or use Properties window, choose Click action . VS or VWD autogenerate this code for us:
protected void Button1_Click(object sender, EventArgs e)
{
//We write code here
}
Add this code:

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
Label2.Text
= "You like: " + TextBox1.Text;
else
Label2.Text
= "You don't like any colors?!!";
}
Run the form, enter color and click Button "Submit", Label 2 will be visible with text "You like: color".

Happy Coding! :)

Comments