This code sample shows first, how to bind a RadioButtonList to a SQL Server DataSource. Then, once an item in the RadioButtonList is selected, a search is made from its DataValueField and returns a list of items matching its search criteria to a DataGrid.
As usual, this uses the SQL Server flavor of the Northwind database, so all you need to do to make this work at home is to copy the code to a new page and change the data connection properties.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>Untitled</title>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=NorthWind"
Dim sql as string = "Select EmployeeID, FirstName + ‘ ‘ + LastName as Name from Employees"
Dim conn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(sql, conn)
conn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
rb1.DataSource = objDR
rb1.datavaluefield="EmployeeID"
rb1.dataTextField="Name"
rb1.DataBind()
end if
End Sub
Sub doit(Source as Object, E as EventArgs)
Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=NorthWind"
Dim sql as string = "Select EmployeeID, FirstName + ‘ ‘ + LastName as Name, Title, BirthDate from Employees where EmployeeID=" & rb1.selecteditem.value
Dim conn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(sql, conn)
conn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
MyDataGrid.DataSource = objDR
MyDataGrid.DataBind()
End Sub
</script>
</head>
<body>
<Form id="form1" runat="server">
<table> <tr>
<td align="Left" valign="Top">
<asp:RadioButtonList id="rb1" cellpadding="0" cellspacing="0" repeatdirection="vertical" runat="server" />
<asp:Button id="button1" Text="Get Item" onclick="doit" runat="server" />
</td>
<td align="Left" valign="Top">
<asp:Datagrid runat="server"
Id="MyDataGrid"
GridLines="Both"
cellpadding="0"
cellspacing="0"
Headerstyle-BackColor="#8080C0"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Size="12"
Headerstyle-Font-Bold="true"
BackColor="#8080FF"
Font-Name="Arial"
Font-Size="10"
BorderColor="Black"
AutogenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="EmployeeID" SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Title" SortExpression="Title" HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="BirthDate" SortExpression="BirthDate" HeaderText="BirthDate"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</Form>
</body>
</html>