- 热门文章:
- · DPC:Creating a DataBound List of Radio Buttons--预览页面source code[等级:中]
- · .NET几大热点问题(转)
- · DataList控件也玩分页 (转自aspcn.com)
- · 用 StringBuilder 类替代 String
- · 杂志目录(页面部分CodeBehind)
- · 杂志目录(数据库访问部分)
- · 杂志目录(页面部分)
- · 推荐一个免费的CSharp编辑器
- · 使用JScript.NET创建asp.net页面(二)
- · 使用JScript.NET创建asp.net页面(三)
- · 使用JScript.NET创建asp.net页面(四)
- · 使用JScript.NET创建asp.net页面(五)
DPC:Creating a DataBound List of Radio Buttons--PART2[等级:中]
Responding to a User@#s Radio Button Choice
What we@#d like to accomplish is to allow the user to choose a radio button option, click a button, and have the chosen publisher@#s books appear. So, to accomplish this, we@#ll need to first add a button control after the radio button list control in our HTML section:
<html>
<body>
<form runat="server">
<b>Choose a Publisher@#s Books to View</b><br>
<asp:radiobuttonlist id="radlstPubs" runat="server"
DataValueField="PublisherID" DataTextField="Name" />
<br>
<asp:button id="btnViewBooks" runat="server"
Text="View Published Books" OnClick="btnViewBooks_Click" />
</form>
</body>
</html>
Note that the btnViewBooks button control@#s OnClick event handler was wired up to the btnViewBooks_Click event handler. This is an event handler that we@#ll create in our server-side script block. This event handler will need to ascertain what radio button option was selected and construct a SQL query based upon that information. (This SQL query will snag all of the books out of the tblBooks table whose PublisherID matches the ID of the selected publisher from the radio button list.) Furthermore, a DataReader (or DataSet) should be populated with the results of this custom SQL query, and that DataReader should be bound to a DataGrid Web control in order to display the books for the publisher.
So, we@#ll need one more Web control, a DataGrid, which will be used to display the books published by the selected publisher. The DataGrid Web control I used can be seen below, it should come after the button control (which has been removed for brevity). Feel free to make any stylistic changes to the DataGrid:
<html>
<body>
<form runat="server">
... CONTENT REMOVED FOR BREVITY ...
<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>
</body>
</html>
Notice that a label Web control, lblTitle, was also added. This will be used to display a nice-looking heading above the DataGrid Web control.
All that@#s left to tackle is writing the btnViewBooks_Click event handler, which will fire whenever the user clicks the btnViewBooks button control. Remember that this event handler needs to populate a DataReader with a list of books that were published by the selected publisher.
... PAGE_LOAD EVENT HANDLER REMOVED FOR BREVITY ...
Sub btnViewBooks_Click(sender as Object, e as EventArgs)
@#If the user has not selected an item from the radiobuttonlist,
@#do nothing
If radlstPubs.SelectedItem Is Nothing then Exit Sub
@#Create a connection
Dim myConnection as New _
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
@#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>
[View a live demo!]
In the btnViewBooks_Click event handler we begin by making sure that a radio button option was selected. If it wasn@#t, we simple exit the event handler. Next, we connect to the database and populate a SqlDataReader with the books that were published by the selected publisher. Note that we are creating a custom SQL string, returning records where the PublisherID column in the tblBooks table is equal to the selected radio button@#s Value property (which, if you recall, was the PublisherID of the tblPublishers table (the primary key)).
Once we have our populated SqlDataReader, we can simply bind it to the dgBooks DataGrid Web control. This, as you can see, takes only two lines of code! Finally, we set the lblTitle label control to a descriptive title. And... we@#re done! That@#s it!
Conclusion
All in all, we created a very useful and visually appealing data-driven Web application in a matter of minutes! Amazing. Of course, radio button lists are not the only form element that can be data bound. Listboxes (see Creating Databound DropDown Lists in ASP.NET) and checkbox lists can also be data bound in a similar fashion. Well, I hope you learned something new and are as excited about ASP.NET as I am!
Happy Programming!
相关文章:
- · 使用JScript.NET创建asp.net页面(六)
- · 使用JScript.NET创建asp.net页面(七)
- · 有好东西不要自己藏着,拿出来大家分享啊,这是我的:关于bate2的MD5加密方法.
- · 转:ASP.NET验证控件详解
- · 在ASP.NET中获取文件属性
- · datalist分页(codebehind部分)
- · datalist分页(cj168.DataAccess.MagsDB中的2个方法)
- · datalist分页(页面部分)
- · .net beta2 操作cookie的例子(转)
- · 如何在DataGrid控件中隐藏列
- · Common ASP.NET Code Techniques (DPC&DWCReference)--1
- · Common ASP.NET Code Techniques (DPC&dwc Reference)--2
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--3
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--4
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--5
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--6
- · 在datagrid中删除时确定(精华区的补充)
- · Displaying ListView items - with class! by Rob Birdwell
- · 一个带checkbox的webcontrol
- · 保护 XML Web 服务免受黑客攻击 [第一部分]
- · 保护 XML Web 服务免受黑客攻击, [第二部分]
- · treeview的源代码
- · 在datagrid中删除时确定?(转)
- · vs.net beta 2中利用DataGrid分页详解
- · COM组件对象与.NET类对象的相互转换
- · 关于DataGrid对象的属性设置(VB)
- · XmlNodeList
- · 用 FormsAuthentication.SetAuthCookie 做权限验证
- · 在datalist中选取数据。
- · Finding a Control Inside a Template
- · DataBinding DropDownList
- · 用asp.net向其他服务器post一条信息
- · Asp.net中用核选框显示数据的方法及ButtonColumn的使用方法
- · 如何在DataGrid控件中实现编辑、删除、分类以及分页操作
- · .NET框架类览胜( Ccident Net )
- · 使用JScript.NET创建asp.net页面(一)
- · 用Visual C#获得计算机名称和IP地址(转)
- · 据说可能是介绍 web.config 最详细的文章。大家参考参考[转]
