DPC:Creating a DataBound List of Radio Buttons--预览页面sou

来源:岁月联盟 编辑:zhu 时间:2003-07-12
&lt;% @Import Namespace=&quot;System.Data&quot; %&gt;<br>
&lt;% @Import Namespace=&quot;System.Data.SqlClient&quot; %&gt;<br>
&lt;script language=&quot;vb&quot; runat=&quot;server&quot;&gt;<br>
&nbsp;&nbsp;Sub Page_Load(sender as Object, e as EventArgs)<br>
&nbsp;&nbsp;&nbsp;&nbsp;If Not Page.IsPostBack then<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BindData()<br>
&nbsp;&nbsp;&nbsp;&nbsp;End If&nbsp;&nbsp;<br>
&nbsp;&nbsp;End Sub<br>
<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;Sub BindData()<br>
&nbsp;&nbsp;&nbsp;&nbsp;'1. Create a connection<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings(&quot;connectionString&quot;))<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;'2. Create the command object, passing in the SQL string<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Const strSQL as String = &quot;SELECT PublisherID, Name FROM tblPublishers ORDER BY Name&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim myCommand as New SqlCommand(strSQL, myConnection)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open()<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;radlstPubs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)<br>
&nbsp;&nbsp;&nbsp;&nbsp;radlstPubs.DataBind()&nbsp;&nbsp;<br>
<br>
&nbsp;&nbsp;End Sub<br>
<br>
<br>
<br>
&nbsp;&nbsp;Sub btnViewBooks_Click(sender as Object, e as EventArgs)<br>
&nbsp;&nbsp;&nbsp;&nbsp;'If the user has not selected an item from the radiobuttonlist,<br>
&nbsp;&nbsp;&nbsp;&nbsp;'do nothing<br>
&nbsp;&nbsp;&nbsp;&nbsp;If radlstPubs.SelectedItem Is Nothing then Exit Sub<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;'1. Create a connection<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings(&quot;connectionString&quot;))<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;'2. Create the command object, passing in the SQL string<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim strSQL as String = &quot;SELECT Title, Description FROM tblBooks &quot; & _<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; WHERE PublisherID = &quot; & radlstPubs.SelectedItem.Value & _<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; ORDER BY Title&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim myCommand as New SqlCommand(strSQL, myConnection)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open()<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;dgBooks.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)<br>
&nbsp;&nbsp;&nbsp;&nbsp;dgBooks.DataBind()&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;lblTitle.Text = &quot;Books Published by &quot; & radlstPubs.SelectedItem.Text<br>
&nbsp;&nbsp;End Sub<br>
&lt;/script&gt;<br>
<br>
&lt;html&gt;<br>
&lt;body&gt;<br>
<br>
&nbsp;&nbsp;&lt;h1&gt;Radio Button List Demo&lt;/h1&gt;<br>
&nbsp;&nbsp;This demo illustrates how to use data-binding to dynamically<br>
&nbsp;&nbsp;create a radio button list based on database information.<br>
&nbsp;&nbsp;The data below is from the<br>
&nbsp;&nbsp;&lt;a href=&quot;http://www.4guysfromrolla.com/webtech/chapters/&quot;&gt;Sample Chapters Database&lt;/a&gt;.<br>
&nbsp;&nbsp;First, the radio button list is bound to the &lt;code&gt;tblPublishers&lt;/code&gt; table.&nbsp;&nbsp;Then,<br>
&nbsp;&nbsp;when you select a publisher, a DataGrid Web control is populated with<br>
&nbsp;&nbsp;the books provided by the selected publisher.&nbsp;&nbsp;(Adding paging to the DataGrid would be<br>
&nbsp;&nbsp;a snap.&nbsp;&nbsp;Just read: &lt;a href=&quot;http://www.4guysfromrolla.com/webtech/072101-1.shtml&quot;&gt;Paing<br>
&nbsp;&nbsp;Database Results in ASP.NET&lt;/a&gt;!)<br>
&nbsp;&nbsp;&lt;p&gt;&lt;hr&gt;&lt;p&gt;<br>
<br>
&nbsp;&nbsp;&lt;form runat=&quot;server&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;b&gt;Choose a Publisher's Books to View&lt;/b&gt;&lt;br&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:radiobuttonlist id=&quot;radlstPubs&quot; runat=&quot;server&quot; Font-Name=&quot;Verdana&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataValueField=&quot;PublisherID&quot; DataTextField=&quot;Name&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:button id=&quot;btnViewBooks&quot; runat=&quot;server&quot; Font-Name=&quot;Verdana&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text=&quot;View Published Books&quot; OnClick=&quot;btnViewBooks_Click&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;p align=&quot;center&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:label id=&quot;lblTitle&quot; runat=&quot;server&quot; Font-Name=&quot;Verdana&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Font-Size=&quot;Large&quot; Font-Bold=&quot;True&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:datagrid id=&quot;dgBooks&quot; runat=&quot;server&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Font-Name=&quot;Verdana&quot; Font-Size=&quot;Smaller&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeaderStyle-BackColor=&quot;Purple&quot; HeaderStyle-ForeColor=&quot;White&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeaderStyle-Font-Size=&quot;Small&quot; HeaderStyle-Font-Bold=&quot;True&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AutoGenerateColumns=&quot;False&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Columns&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:BoundColumn HeaderText=&quot;Book Title&quot; HeaderStyle-HorizontalAlign=&quot;Center&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataField=&quot;Title&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:BoundColumn HeaderText=&quot;Synopsis&quot; HeaderStyle-HorizontalAlign=&quot;Center&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataField=&quot;Description&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/Columns&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/asp:datagrid&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/p&gt;<br>
&nbsp;&nbsp;&lt;/form&gt; <br>
<br>