<% @import namespace="system.data" %> <% @import namespace="system.data.sqlclient" %> <script language="vb" runat="server"> sub page_load(sender as object, e as eventargs) if not page.ispostback then binddata() end if end sub
sub binddata() '1. create a connection dim myconnection as new sqlconnection(configurationsettings.appsettings("connectionstring"))
'2. create the command object, passing in the sql string const strsql as string = "select publisherid, name from tblpublishers order by name" dim mycommand as new sqlcommand(strsql, myconnection)
sub btnviewbooks_click(sender as object, e as eventargs) 'if the user has not selected an item from the radiobuttonlist, 'do nothing if radlstpubs.selecteditem is nothing then exit sub
'1. create a connection dim myconnection as new sqlconnection(configurationsettings.appsettings("connectionstring"))
'2. create the command object, passing in the sql string dim strsql as string = "select title, description from tblbooks " & _ " where publisherid = " & radlstpubs.selecteditem.value & _ " order by title" dim mycommand as new sqlcommand(strsql, myconnection)
lbltitle.text = "books published by " & radlstpubs.selecteditem.text end sub </script>
<html> <body>
<h1>radio button list demo</h1> this demo illustrates how to use data-binding to dynamically create a radio button list based on database information. the data below is from the <a href="http://www.4guysfromrolla.com/webtech/chapters/">sample chapters database</a>. first, the radio button list is bound to the <code>tblpublishers</code> table. then, when you select a publisher, a datagrid web control is populated with the books provided by the selected publisher. (adding paging to the datagrid would be a snap. just read: <a href="http://www.4guysfromrolla.com/webtech/072101-1.shtml">paing database results in asp.net</a>!) <p><hr><p>
<form runat="server">
<b>choose a publisher's books to view</b><br> <asp:radiobuttonlist id="radlstpubs" runat="server" font-name="verdana" datavaluefield="publisherid" datatextfield="name" /> <br> <asp:button id="btnviewbooks" runat="server" font-name="verdana" text="view published books" onclick="btnviewbooks_click" />