- 热门文章:
- · DPC:Creating a DataBound List of Radio Buttons--PART2[等级:中]
- · 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页面(四)
DPC:Creating a DataBound List of Radio Buttons--PART1[等级:中]
--------------------------------------------------------------------------------
For More Information on ASP.NET
This article examines how to create a databound listbox using ASP.NET. For more information on ASP.NET be sure to check out the articles in the ASP.NET Article Index. The code in this article is based on the Beta 2 version of ASP.NET. To learn more about the free ASP.NET Beta 2, be sure to check out this FAQ.
Introduction
A common task Web developers face is creating a form element (such as a series of checkboxes, radio buttons, or a listbox) whose members are populated by the contents of a database table. This is quite possible (and quite easy) to accomplish with ASP.NET In fact, in a previous article (Creating Databound DropDown Lists in ASP.NET), we examined how to create a databound listbox. In this article, we@#ll examine how to create a series of radio buttons whose values are automagically populated from a database table! We@#ll also look at how to respond to the user selecting a particular option and showing more detailed information based upon the radio button selected.
The Impetus
On 4Guys there is a Sample On-Line Chapters page, which lists a number of books that have free, on-line sample chapters. All of this information is stored in a database that contains two tables: tblPublishers and tblBooks. The tblPublishers contains a row for each publisher that has a book that appears in the sample chapters listing, while the tblBooks table contains a row for each sample book. Note that the tblBooks table contains a PublisherID column which serves as a foreign key to the tblPublishers table, tying a particular publisher to a particular book.
Wouldn@#t it be nice to generate a list of radio buttons based upon the publishers in the tblPublishers table? From this list, the user could select a particular publisher, at which point, the user would be shown a listing of books published by that particular publisher. In creating the radio button list, we could hard code the list of publishers, but that would be subject to change any time we added or removed publishers from the tblPublishers table. Ideally, the radio button list would be dynamically generated based upon the values in the tblPublishers table.
If you@#d like to see what the end result will look like, check out the CheckboxListDemo.aspx">live demo. Pretty nice looking, eh? Realize that the radio button list was created dynamically using databind (essentially three lines of code in total). And this entire page, from start to finish, took me about four minutes to write. (I type fast. :-))
Creating the Databound Radio Button List
Creating a databound series of radio buttons is painfully simply with ASP.NET. We only really need to do three things:
Create the asp:radiobuttonlist Web control, specifying the database columns we want to bind to the radio buttons@# displayed text values and the hidden values that are passed along when the form is submitted.
Write the code to populate a DataReader (or DataSet) with the applicable database information.
Databind the DataReader (or DataSet) to the asp:radiobuttonlist Web control
That@#s it! So, let@#s examine each of these three steps. First, create the needed Web control in the HTML section of your ASP.NET Web page.
<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" />
</form>
</body>
</html>
The two most important things to notice is that we set the asp:radiobuttonlist Web control@#s DataValueField and DataTextField properties. Both properties should map to a column name in the database table that we wish to bind the radio button list to. The DataTextField property are the names that will be visually displayed next to each radio button; the DataValueField are the hidden values that are associated with each radio button. Also note that the radio button list was placed within a WebForm (the form tag with a runat="server" attribute).
Now that we@#ve tackled our first step, let@#s look at performing our remaining two steps: reading the database table tblPublishers into a DataReader and binding the populated DataReader to the radio button list!
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
@#*** STEP 1: Populate a DataReader ***
@#Create a connection
Dim myConnection as New _
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
@#Create the command object, passing in the SQL string
Const strSQL as String = "SELECT PublisherID, Name FROM tblPublishers"
Dim myCommand as New SqlCommand(strSQL, myConnection)
@#Open the connection and set the DataSource to the
myConnection.Open()
@#Populate the datareader
Dim myDataReader as SqlDataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
@#*** STEP 2: Bind the DataReader to radlstPubs ***
radlstPubs.DataSource = myDataReader
radlstPubs.DataBind()
End If
End Sub
...
</script>
In the above Page_Load event handler we accomplish our two steps: populating a DataReader (in this case a SqlDataReader) with the contents of the tblPublishers table; and binding the resulting DataReader to our radio button list, radlstPubs. Note that we only wish to run this binding code on the first visit to the page (more on why later), hence we only perform the databinding if Page.IsPostBack is False, meaning that the page has not yet been posted back.
Now that we@#ve looked at how to create a databound radio button list, let@#s turn our attention to responding to the user@#s selection of a particular radio button. In Part 2 we@#ll examine this aspect and the remainder of our application!
相关文章:
- · 使用JScript.NET创建asp.net页面(五)
- · 使用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地址(转)
