首页 > 开发 > 综合 > 正文

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

2024-07-21 02:16:50
字体:
来源:转载
供稿:网友

&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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表