.net教程:dpc:creating a databound list of radio buttons--预览
<% @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)
myconnection.open()
radlstpubs.datasource = mycommand.executereader(commandbehavior.closeconnection)
radlstpubs.databind()
end sub
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)
myconnection.open()
dgbooks.datasource = mycommand.executereader(commandbehavior.closeconnection)
dgbooks.databind()
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" />
<p align="center">
<asp:label id="lbltitle" runat="server" font-name="verdana"
font-size="large" font-bold="true" />
<asp:datagrid id="dgbooks" runat="server"
font-name="verdana" font-size="smaller"
headerstyle-backcolor="purple" headerstyle-forecolor="white"
headerstyle-font-size="small" headerstyle-font-bold="true"
autogeneratecolumns="false">
<columns>
<asp:boundcolumn headertext="book title" headerstyle-horizontalalign="center"
datafield="title" />
<asp:boundcolumn headertext="synopsis" headerstyle-horizontalalign="center"
datafield="description" />
</columns>
</asp:datagrid>
</p>
</form> <