This is version 2 of a previous sample here that was done using ASP.Net v1.x. This one, of course, is created using the features of ASP.Net v2.
With the advent of ASP.Net 2.0, the RadioButtonList and the DropDownList (among others) have been updated so that population may occur with a DataSource control.
The initial page loads the RadioButtonList. Next, you will need to select an item from the RBL. This will then populate the DropDownList.
Notice, too, that, as with the previous sample, the DropDownList population is accomplished with a Stored Procedure. Since the SProc needs a parameter, this is accomplished by adding a ControlParameter to the SQLDataSource that populates the DDL.
In this Control Parameter, the Name property is the Parameter needed by the Stored Procedure. The ControlID property refers to the RadioButtonList’s ID property and lastly, the PropertyName is set to ‘SelectedValue’ – the Selected Item of the RadioButtonList, used to populate the DropDownList.
This all probably sounds more complicated than it really is….:)
Just check out the code below to see how easy it is to put something like this together.
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>Populating a DropDownList from a RadioButtonList (v2.0)</title>
<script language="VB" Runat="server">
Sub GetCategory(Source as Object, E as EventArgs)
ddl.Visible="True"
lblProduct.text="Products in " & rb1.selecteditem.text & " Category"
label1.text= ("Selected Value = " & rb1.selecteditem.value )
End Sub
</script>
</head>
<body>
<Form id="form1" runat="server">
<table border="0">
<tr>
<td align="Left" valign="Top"><b><i>Categories:</i></b><br>
<asp:RadioButtonList id="rb1"
datatextfield="CategoryName"
datavaluefield="CategoryID"
DataSourceID="sqlDSCategories"
AutoPostBack="True"
OnSelectedIndexChanged="GetCategory"
runat="server">
</asp:RadioButtonList>
</td>
<td align="Left" valign="Top">
<b><i><asp:Label ID="lblProduct" runat="server" /></i></b><br>
<asp:DropDownList id="ddl" DataKeyNames="ProductID"
Datatextfield="Productname"
DataSourceID="SQLDS2"
Visible="False" runat="server" />
</td>
</tr>
</table>
<asp:Label ID="label1" Font-Italic="True" Font-Bold="True" Runat="server" />
<asp:SQLDataSource ID="sqlDSCategories"
Runat="Server"
SelectCommand = "SELECT CategoryID, CategoryName From Categories"
ConnectionString="server=YourSrvr;Database=YourDB;pwd=YourPWD;uid=YourUID;">
</asp:SQLDataSource>
<asp:SqlDataSource ID="SQLDS2" runat="server"
SelectCommand="GetProductsByCategory"
SelectCommandType="StoredProcedure"
ConnectionString="server=YourSrvr;Database=YourDB;pwd=YourPWD;uid=YourUID;">
<SelectParameters>
<asp:ControlParameter
ControlID="rb1"
Name="CatID"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</Form>
</body>