DPC:Creating a DataBound List of Radio Buttons--预览
2024-07-21 02:16:50
供稿:网友
<% @import namespace="system.data" %><br>
<% @import namespace="system.data.sqlclient" %><br>
<script language="vb" runat="server"><br>
sub page_load(sender as object, e as eventargs)<br>
if not page.ispostback then<br>
binddata()<br>
end if <br>
end sub<br>
<br>
<br>
sub binddata()<br>
'1. create a connection<br>
dim myconnection as new sqlconnection(configurationsettings.appsettings("connectionstring"))<br>
<br>
'2. create the command object, passing in the sql string<br>
const strsql as string = "select publisherid, name from tblpublishers order by name"<br>
dim mycommand as new sqlcommand(strsql, myconnection)<br>
<br>
myconnection.open()<br>
<br>
radlstpubs.datasource = mycommand.executereader(commandbehavior.closeconnection)<br>
radlstpubs.databind() <br>
<br>
end sub<br>
<br>
<br>
<br>
sub btnviewbooks_click(sender as object, e as eventargs)<br>
'if the user has not selected an item from the radiobuttonlist,<br>
'do nothing<br>
if radlstpubs.selecteditem is nothing then exit sub<br>
<br>
'1. create a connection<br>
dim myconnection as new sqlconnection(configurationsettings.appsettings("connectionstring"))<br>
<br>
'2. create the command object, passing in the sql string<br>
dim strsql as string = "select title, description from tblbooks " & _<br>
" where publisherid = " & radlstpubs.selecteditem.value & _<br>
" order by title"<br>
dim mycommand as new sqlcommand(strsql, myconnection)<br>
<br>
myconnection.open()<br>
<br>
dgbooks.datasource = mycommand.executereader(commandbehavior.closeconnection)<br>
dgbooks.databind() <br>
<br>
lbltitle.text = "books published by " & radlstpubs.selecteditem.text<br>
end sub<br>
</script><br>
<br>
<html><br>
<body><br>
<br>
<h1>radio button list demo</h1><br>
this demo illustrates how to use data-binding to dynamically<br>
create a radio button list based on database information.<br>
the data below is from the<br>
<a href="http://www.4guysfromrolla.com/webtech/chapters/">sample chapters database</a>.<br>
first, the radio button list is bound to the <code>tblpublishers</code> table. then,<br>
when you select a publisher, a datagrid web control is populated with<br>
the books provided by the selected publisher. (adding paging to the datagrid would be<br>
a snap. just read: <a href="http://www.4guysfromrolla.com/webtech/072101-1.shtml">paing<br>
database results in asp.net</a>!)<br>
<p><hr><p><br>
<br>
<form runat="server"><br>
<br>
<b>choose a publisher's books to view</b><br><br>
<asp:radiobuttonlist id="radlstpubs" runat="server" font-name="verdana"<br>
datavaluefield="publisherid" datatextfield="name" /><br>
<br><br>
<asp:button id="btnviewbooks" runat="server" font-name="verdana"<br>
text="view published books" onclick="btnviewbooks_click" /><br>
<br>
<p align="center"><br>
<asp:label id="lbltitle" runat="server" font-name="verdana"<br>
font-size="large" font-bold="true" /><br>
<asp:datagrid id="dgbooks" runat="server"<br>
font-name="verdana" font-size="smaller"<br>
headerstyle-backcolor="purple" headerstyle-forecolor="white"<br>
headerstyle-font-size="small" headerstyle-font-bold="true"<br>
autogeneratecolumns="false"><br>
<br>
<columns><br>
<br>
<asp:boundcolumn headertext="book title" headerstyle-horizontalalign="center"<br>
datafield="title" /><br>
<asp:boundcolumn headertext="synopsis" headerstyle-horizontalalign="center"<br>
datafield="description" /><br>
</columns><br>
</asp:datagrid><br>
</p><br>
</form> <br>
<br>