日期:2013-09-19  浏览次数:20415 次

<% @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 pu