Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

ASP 4.0 – RadioButton Control – Lab #009

ASP 4.0 RadioButton Control

ASP 4.0 RadioButton Control allows you to pick one of many values. Radio buttons are a group of controls. For example, if there are 4 radio buttons, a user can keep only one radio button in a checked state within a group. If there are two groups, the user can keep two buttons in checked state. Means, one button in each group can be in a selected state. In this Lab, we will create a Radio Button group and mention which one is in checked state.

1: Design ASP.40 Form with Radio Buttons

In this step, we will design the form with Radio Buttons. Open the Website from our previous Lab and add a new WebForm called: 05_RadioButtonControl.aspx then follow the steps below:

Step 1 Design ASP 4.0 Form with RadioButton Controls
Step 1 Design ASP 4.0 Form with RadioButton Controls

1) Create a Free Text as shown.
2) Note, we divide the Free Text into two lines using the <br/> tag.
3) Create three RadioButton Control in the WebForm
4) Use the source view shown here to set the properties via Properties Window or edit the attribute in the source window.
5) Add a Button control to the Form. When the user clicks this button, it will report the selected radio button.
6) Marker six shows the Properties of the button control.

2: Group Radio Button & Report Selected One

In this section, we will group all three radio buttons so that the user can check only one button at a time. Follow the steps below:

Step 2 - Radio Button State GroupName Property
Step 2 – Radio Button State GroupName Property

1) Select all three radio buttons and set GroupName property as ProductItem. Now, all three radio buttons are grouped into ProductName.
2) Drag & Drop a Label control to the Web Form. We will use it to report the selected radio button item.
3) The Label Control’s properties are shown here.
4) Handle the Click event of the Save button.
5) In the Click Event handler, we test the Checked Property of the Radio Button and assign the Label text.

3: Run the ASP 4.0 RadioButton Example

In this section, we will test our sample application. Follow the steps below:

Step 3 - Running the Lab Example
Step 3 – Running the Lab Example

1) Run the WebForm and the default Web Server will show web form with Radio Buttons.
2) Pick the radio button Item 3. You can see the check mark added to the selected item. The label is not changed because we handled only the Save Button’s click event. Form data is also not posted to the server as we do not set AutoPostBack property for any of the Radio Buttons.
3)  Select Item 2 radio button. You can notice the web browser removes the check mark from Item 3 and places it on item 2. This is because we group all the radio buttons via the GroupName property.
4) Click on the Save button. This action will submit the data to the server and run all the event routines on the server.
5) Save button handler runs on the server & the label control reports the selected radio button.

Code Reference

05_RadioButtonControl.aspx

05_RadioButtonControl.aspx.cs

Categories: ASP 4.0

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.