For more information on ASP.net
This article examines you to create a databound listbox using ASP.net. For more information on ASP.net is sure to check out of the articles in ASP.net Article Index. The code in this article was based on the Beta 2 version of ASP.net. To learn more about the "free asp.net Beta 2" is 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 Drop Down Lists in asp.net), we examined you 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 dat Abase table! We ' ll also look in how to respond to the user selecting a particular option and showing more detailed information On the radio button selected.
The impetus
On 4Guys There are a Sample on-line chapters page, which lists a number of books this have free, on-line Sample chapters. All of this information are stored in a database that contains two tables:tblpublishers and Tblbooks. The tblpublishers contains a row for each publisher so has a book this appears in the sample chapters listing, while the Tblbooks table contains a row for each of the sample book. Note This 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 is 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 is shown a listing of books p Ublished by that particular publisher. In creating the radio button list, we could hard code the list of publishers, but that would is subject to We added or removed publishers from the Tblpublishers table. Ideally, the radio button list would is dynamically generated based upon the values in the Tblpublishers table.
If you are like "D", what the end result would look like and check out the "Checkboxlistdemo.aspx" >live demo. Pretty nice looking, eh? Realize that the radio button list is created dynamically using DataBind (essentially three lines of code in total). And this is 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 with simply. We only have really need to do three things:
Create the Asp:radiobuttonlist Web control, specifying the database columns we want to bind to the radio buttons ' Displaye D-text values and the hidden values that are passed along then 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. The needed Web control in the HTML section of your asp.net web page.
<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>
The two most important things to notice are that we set the Asp:radiobuttonlist Web control ' s DataValueField and Datatextfi Eld 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 would be visually displayed next to each radio button; The DataValueField are the hidden values that are associated with each radio button. Also Note This radio button list is placed within a webform (the form tag with a runat= "server" attribute).
Now so we ' ve tackled our "I", let's look in performing our remaining two steps:reading the database table Tblpub Lishers 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 ' 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 my two steps:populating a DataReader (in this case a SqlDataReader) W ith the contents of the Tblpublishers table; and binding the resulting DataReader to our radio button list, radlstpubs. That's we only wish to run this binding code on the the the the the the the the the the the "the" page (more on why later), hence we perform The databinding if Page.IsPostBack is False, meaning this Page has not yet been back.
Now so we ' ve looked at how to create a databound radio button list, let's turn we attention to responding to the user ' s Selection of a particular radio button. In Part 2 We'll examine this aspect and the remainder's our Applica
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.