0% found this document useful (0 votes)
23 views5 pages

HW 325781

Uploaded by

saranshsaxena63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

HW 325781

Uploaded by

saranshsaxena63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming in ASP.

NET: Adding Controls to a Web Form


ASP.NET is a popular web development framework that allows developers to create dynamic
web applications. One of the fundamental aspects of web development in ASP.NET is adding
controls to a web form. Controls are elements that provide interaction and functionality on a
web page. Here, we'll explore various controls such as buttons, textboxes, labels, checkboxes,
radio buttons, and list boxes, as well as discuss the different states in ASP.NET and
validation controls.

1. Adding Controls to a Web Form:


• To add controls, you can use the Visual Studio IDE or manually write code in
ASP.NET.
• Controls can be server-side or client-side, each with its own purpose and behavior.

2. Buttons:
• Buttons trigger actions on a web page.
• Common types: Submit, Reset, and Custom buttons.

3. Textboxes:
• Textboxes allow users to input text.
• Useful for capturing user data like names, emails, and more.

4. Labels:
• Labels are used to display text or provide descriptions for other controls.
• They are typically non-interactive.

5. Checkboxes:
• Checkboxes allow users to make binary choices (e.g., agree/disagree).
• Useful for boolean input.

6. Radio Buttons:
• Radio buttons enable users to select one option from a group.
• They are often used for mutually exclusive choices.

7. List Boxes:
• List boxes present a list of options for users to select.
• Options can be single or multiple selections.

The Microsoft.NET Framework provides a rich set of server-side controls for developing
Web applications. You can add these controls to WebForms pages just as you add Windows
controls to a form. Server-side controls are often called server controls or Web Forms
controls. There are four types of Server controls: HTML server controls. Web server controls,
validation control, and user controls.
HTML Server controls

HTML developers must be familiar with old HTML controls, which they use to write GUI
applications in HTML. These controls are the same HTML controls; you can run these
controls on the server by defining the runat ="server" attribute. These control names start
with Html. Table 7-1 defines some of these controls.

HTML server Controls

CONTROL DESCRIPTION
HtmlForm Create an HTML form control,
used as a place holder of other
controls
HtmlInputText Creates an input text box control
used to get input from user
HtmltextArea Creates multiline text box control
HtmlAnchor Creates a Web navigation
HtmlButton Creates a button control
HtmlImage Creates an image control, which
is used to display an image
HtmlInputCheckBox Creates a check box control
HtmlInputRadioButton Creates a radio button control
HtmlTable Creates a table control
HtmlTableRow Creates a row within a table
HtmlTableCell Creates a cell with in a row

Web Server Controls

Web server controls are more powerful than HTML controls because they provide more
functionality and are easier to use. Besides some of the basic controls such as button, text
box, label, and checkbox, ASP.NET provides some more powerful controls such as DataGrid,
DataList, and Calendar. I'll use these controls throughout this article. Table 7-2 describes
some of these controls.

Web Server controls

CONTROL DESCRIPTION
Label Represents a label control
ListBox Represents a list box control
CheckBox Represents a Check box control
Calendar Represents a calendar control
ImageButton Represents an image button control
TableCell Represents a table cell
Panel Represents a panel control
DataList Represents a data list control
TextBox Represents a text box control
Image Represents an image control
CheckBoxList Represents a list box with check boxes
Button Represents a button control
HyperLink Represents a hyperlink control
TableRow Represents a row of a table
RadioButtonList Represents a list box with radio button
controls
DataGrid Represents a data grid control
DropDownList Represents a drop-down list control
AdRotator Represents an ad rotator control
RadioButton Represents a radio button control
LinkButton Represents a link button control
Table Represents a table control
Repeater Represents a repeater control

I'll show how to use these controls in example applications through out this article

Now let us start to create a Website as:

Start - All Programs - Microsoft Visual Studio 2010.


File - New Website - C# - Empty website (to avoid adding a master page).
Give the web site a name such as AddingDynamic_controls or another as you wish and
specify the location.
Then right-click on Solution Explorer - Add New Item - Default.aspx page.
Drag and drop one button on the <form> section of the Default aspx page.
Then switch to design view and double-click on "Button" then in the <form> section of the
Default aspx page source will look like as in the following:

<formid="form1"runat="server">
<div>
<asp:ButtonID="Button1"runat="server"Text="Add"onclick="Button1_Click" />
</div>
</form>
Now switch to Default.aspx.cs and use the following code in the button click event:

protected void Button1_Click(object sender, EventArgs e)


{
for (inti = 0; i< 4; i++) //specifying the condition in for loop for creating no. of
textboxes
{

TextBoxtb=new TextBox (); //Create the object of TexBox Class


tb.ID = i.ToString(); // assign the loop value to textbox object for dynamically
adding Textbox ID
Form.Controls.Add(tb); // adding the controls

}
}
Now run the application and click on the add button. It will add four Textboxes into the
running web page dynamically as shown in the following image:

dynamic.jpg

In the above example you see that the four textboxes are added to the form which were not
defined at the time of form design. You can create any type of control dynamically; suppose

if you want to create a Dropdwonlist dynamically then use the following code in the button
click:

DropDownListdb=new DropDownList (); //Create DropDownList

Similar to this, you can create any web control.

You might also like