Dpc:creating a databound List of Radio buttons--preview page Source code[rank: in

Source: Internet
Author: User
Page <% @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 a 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>
<body><br>
<br>
This demo illustrates you 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>
The radio button list is bound to the <code>tblPublishers</code> table. Then,<br>
When you are 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><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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.