ASP.NET building Forms with webcontrols part three

7:13 PM Posted by Isha

ASP.NET web forms can have web controls can be build and control at the server with the help of programming. We have previously discussed regarding active server page forms with web controls part one and asp.net forms part two and this post is in continuation of it.

Using Web Controls:

You can add ASP.NET server controls to a Web Forms page by using either of the following two features:
  1. The Web Forms section of the toolbox
  2. The ASP.NET code
You add controls from the toolbox in Design view of the Web Forms page (the ASPX file). The toolbox categorizes the different types of controls in separate tabs, such as Web Forms, HTML, Components, and Data. You can use the HTML tab to add HTML controls and use the Web Forms tab to add the ASP.NET server controls to Web Forms.

However, to make the HTML controls available for coding at the server end, these controls need to be converted to server controls. To do so, right-click the HTML control on the page and select Run As Server Control from the context menu.

While selecting between HTML and Web server controls, using Web server controls is preferred,because they provide a rich object model and are adaptable to multiple browsers depending on browser capabilities. However, HTML server controls are preferred when migrating from the existing ASP pages to ASP.NET pages, because, unlike Web server controls, HTML server controls map directly to HTML tags.

You can also add a Web control to a page by using the ASP.NET code. You can access the ASP.NET code in the HTML view of the page (ASPX file).

You can add ASP.NET server controls by using an Extensible Markup Language (XML) tag referenced as asp.

You can also programmatically add a control at run time. The following VB.NET code snippet demonstrates how to add a TextBox control at run time:

Dim TextBox1 as New TextBox()

Controls.Add(TextBox1)

Every control has specific properties and methods. You can set control properties to modify the appearance or behavior of controls. For example, you can set the font, color, and size of a control. You can use the control methods to perform a specific task, such as moving a control. You can set control properties at design times by using the Properties window or at run time by using the code. Every control has a property called ID that is used for the unique identification of the control. You can set the property of a control at run time by using the following syntax:

ControlID.PropertyName=Value

In this syntax:

ControlID represents the ID property of the control.

PropertyName represents the control property.

Value represents the value assigned to PropertyName, which is a control's property.

CheckBox and CheckBoxList controls

Check boxes provide you with independent choices or options that you can select. You can add check boxes to a Web Forms page by using either the CheckBox control or the CheckBoxList control. The CheckBox control is a single check box that you can work with. On the other hand, the CheckBoxList control is a collection of several check boxes.

After you add the CheckBoxList control, you need to add a list of items to it. To do so:

1. Display the Properties window of the CheckBoxList control.

2. Click the ellipsis button for the Items property of the CheckBoxList control.

3. In the ListItem Collection Editor dialog box, click Add to create a new item. A new item is created and its properties are displayed in the Properties pane of the dialog box.

4. Verify that the item is selected in the Members list, and then set the item properties. Each item is a separate object and has following properties:

Text: Represents the text to be displayed for the item in the list.

Value: Represents the value associated with the item without displaying it. For example, you can set the Text property of an item as the city name and the Value property to the postal code of the city. Thus, you can keep the Text and Value properties different when you do not want the actual value to be displayed
to the user.

Selected: A Boolean value that indicates whether or not the item is selected.

RadioButton and RadioButtonList controls

Radio buttons provide a set of choices or options that you can select. You can add radio buttons to a Web Forms page by using either the RadioButton control or the RadioButtonList control. The RadioButton control is a single radio button. On the other hand, the RadioButtonList control is a collection of radio buttons. Radio buttons are seldom used singly.

Usually, you use radio buttons in a group. A group of radio buttons provides a set of mutually exclusive options — you can select only one radio button in a group. You can group a set of radio buttons in two ways:

Place a set of RadioButton controls on the page and assign them manually to a group. To do so, you can use the GroupName property.

Place a RadioButtonList control on the page; the radio buttons in the control are automatically grouped.

After you add a RadioButtonList control, you need to add the individual radio buttons.

You can do so by using the Items property in the same way as you do for the CheckBoxList control.

You add the items to a RadioButtonList control at run time in the same way as you add items to a CheckBoxList control. The following VB.NET code snippet demonstrates how to add items to a RadioButtonList control programmatic ally:

Dim RadioButtonList1 As New RadioButtonList()
Controls.Add(RadioButtonList1)
RadioButtonList1.Items.Add("Radio1")

You can use the Checked property of the RadioButton control to identify whether or not the control is selected. For the RadioButtonList control, you can access the index of the selected item by using the SelectedIndex property and access the selected item by using the SelectedItem property of the control.

Related Posts:

Building forms with asp.net part one
and two

0 comments:

Post a Comment