Dpc:creating a databound List of Radio buttons--preview

Source: Internet
Author: User
<% @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 a 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>

<body>

This demo illustrates you 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>.
The radio button list is bound to the <code>tblPublishers</code> table. Then,
When you are select a Publisher, a DataGrid Web control are populated with
The books provided by the selected publisher. (Adding paging to the DataGrid would is
A snap. Just read: <a href= "http://www.4guysfromrolla.com/webtech/072101-1.shtml" >paing
Database Results in asp.net</a>!)
<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"/>

<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> <




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.