html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

DPC:Creating a DataBound List of Radio Buttons--预览页面source code[等级:中]_.NET教程_编程技术

[ ] 已经帮助:人解决问题
页面
<% @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>
9mYHTML5中文学习网 - HTML5先行者学习网
9mYHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助