RadioButtonList with AutoPostback

This sample uses the SQL Managed Provider and the Northwind Database to, based on the selection from the RadioButtonList (Manually populated, in this case), populate a DropDownList with all the Product Names that fall in the selected Category. The AutoPostback Property, being set to ‘True’, takes the place of the button. When you click on the RadioButtonList item, this tells it to automatically postback and run the sub or function designated in the onselectedindexchanged method.

Just copy the code into new page on your site and change the database connection information.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 2.1">
	<title>Populating a DropDownList from a RadioButtonList</title>
<script language="VB" runat="server">

Sub popddl(Source as Object, E as EventArgs)
	Dim strConn as string = "server=YourServer;uid=YourUID;pwd=YourPWD;database=Northwind"
	Dim MySQL as string = "Select ProductName from NWProducts Where CategoryID = @CatID"
	Dim MyConn as New SQLConnection(strConn)
	Dim objDR as SQLDataReader
	Dim Cmd as New SQLCommand(MySQL, MyConn)
	cmd.parameters.add(New SQLParameter("@CatID", rb1.selecteditem.value))
	MyConn.Open()
	objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
	ddl.DataSource = objDR
	ddl.DataBind()
	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" AutoPostback="true" OnSelectedIndexChanged ="popddl" runat="server">
		<ASP:LISTITEM value="1">Beverages</ASP:LISTITEM>
		<ASP:LISTITEM value="2">Condiments</ASP:LISTITEM>
		<ASP:LISTITEM value="3">Confections</ASP:LISTITEM>
		<ASP:LISTITEM value="4">Dairy Products</ASP:LISTITEM>
		<ASP:LISTITEM value="5">Grains/Cereals</ASP:LISTITEM>
		<ASP:LISTITEM value="6">Meat/Poultry</ASP:LISTITEM>
		<ASP:LISTITEM value="7">Produce</ASP:LISTITEM>
		<ASP:LISTITEM value="8">Seafood</ASP:LISTITEM>
		</asp:RadioButtonList>
		</td>
		<td align="Left" valign="Top">
		<b><i><asp:Label ID="lblProduct" runat="server" /></i></b><br>
		<asp:DropDownList id="ddl" datatextfield="Productname" visible="false" runat="server" />
		</td>
	</tr>
</table>
</Form>
<asp:Label ID="label1"  runat="server" />
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>