Skip Navigation LinksASP.Net 2.0 Training : ASP.Net RadioButton
Banner for ASP.Net 2.0 training classes in Visual Basic 2005 

Skip Navigation Links
Visit my other site:

certification training

Free Flash tutorials and training

Free Dreamweaver Video Tutorials

Free PHP Training

Visual Basic Windows Training




If you like this site please link to it


RadioButton

  • The RadioButton control is used to display a radio button.
  • RadioButtons are usually used in groups to allow the user to pick one from the group.
  • All radioButtons in the same group must have the same groupName property

Property

Description

AutoPostBack

A Boolean value that specifies whether the form should be posted immediately after the Checked property has changed or not. Default is false

Checked

A Boolean value that specifies whether the radio button is checked or not

Id

A unique id for the control

GroupName

The name of the group to which this radio button belongs

OnCheckedChanged

The name of the function to be executed when the Checked property has changed

runat

Specifies that the control is a server control.  Must be set to "server"

Text

The text next to the radio button

TextAlign

On which side of the radio button the text should appear (right or left)

 

Try this:

1

<script  runat="server">

2

Sub submit(Sender As Object, e As EventArgs)

3

if red.Checked then

4

   Label1.Text="You selected " & red.Text

5

elseIf green.Checked then

6

   Label1.Text="You selected " & green.Text

7

elseIf blue.Checked then

8

   Label1.Text="You selected " & blue.Text

9

end if

10

End Sub

11

</script>

12

<html><body>

13

<form runat="server">

14

Select your favorite color:

15

<br />

16

<asp:RadioButton id="red" Text="Red" Checked="True" GroupName="colors" runat="server"/>

17

<br />

18

<asp:RadioButton id="green" Text="Green" GroupName="colors" runat="server"/>

19

<br />

20

<asp:RadioButton id="blue" Text="Blue" GroupName="colors" runat="server"/>

21

<br />

22

<asp:Button text="Submit" OnClick="submit" runat="server"/>

23

<p><asp:Label id="Label1" runat="server"/></p>

24

</form>

25

</body></html>

Save as: RadioButton.aspx

Determining if radioButton was checked:

If radioButtonName.checked then

‘do code if checked

End if

  • Open Visual Studio
  • Add a new webform
  • Name it radioEx.aspx
  • Click the Add button
  • Select the Design View
  • Add a label to the webform, change the text property to Select your favorite color. Change the id to lblPrompt
  • Add an 3 radioButton controls to the webform

  • Change the id of the radioButtons to red, green, blue
  • Change the text of the radioButtons to Red,Green, Blue
  • Set the autoPostBack property for all 3 radioButtons to true
  • Press the control key and select each of the radioButton controls then change the groupName of all of them to Colors
  • Change the checked property of the first radioButton to true

  • Add another label below these controls and change the id to lblOutput, clear the text property
  • Double-click the first radioButton
  • Look at the subroutine signature:

Protected Sub red_CheckedChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles red.CheckedChanged

Visual Studio creates a subroutine and named it red_checkedChanged that handles the checkedChanged event of the red radioButton control

  • Change the subroutine name to getChanged.
  • Add a comma at the end and add green.checkedChanged
  • Add another comma and add Blue.checkChanged

This will set this one subroutine to handle all 3 radioButton’s checkedChanged event

Protected Sub getChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles red.CheckedChanged, blue.CheckedChanged, Green.CheckedChanged

Add the following code to this subroutine:

If sender.Checked Then

lblOutput.Text = "You selected " & sender.Text

End If

Notice you are using the text from the sender object which will be passed into the subroutine

  • test it

      ASP.Net 2.0 tutorials & training