Learning More About Binding Data to Controls
(Page 1 of 4 )
Looking for a way to writer fewer lines of code while streamlining your ASP.NET application? This article, the second of two parts, shows you one way to do this. It is excerpted from chapter 12 of
ASP.NET 2.0 DeMYSTiFieD, written by Jim Keogh (McGraw-Hill/Osborne; ISBN: 0072261412).
Radio Button
As you learned in Chapter 8, radio buttons are a convenient way to display a group of options from which the visitor selects only one option within the group.
You can store in a table names of radio buttons that are within the same group. This enables you to dynamically define members of the group according to values in the database. For example, a customer might qualify for a unique set of delivery options, as shown by the customer’s profile. The set can be stored in a table.
Data contained in a table is linked to a radio button using the same techniques as are used to link the DropDownList control. For instance, you can assign the column name to the DataTextField property of the radio button control as shown in the next example. For the following example, you’ll need to modify the custContact table to include the DeliveryOption column before running this program.
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
If Not IsPostBack Then
Dim conCust As SqlConnection
Dim cmdSelectRows As SqlCommand
Dim dtrCust As SqlDataReader
conCust = New SqlConnection( "Server=localhost;UID=
MyID;PWD=MyPassword;Database=CustomerContact Data")
cmdSelectRows = New SqlCommand( "Select DeliveryOption
From custContact Where Critera='45'", conCust)
conCust.Open()
dtrCust = cmdSelectRows.ExecuteReader()
radioButtonSelection.DataSource = dtrCust
radioButtonSelection.DataTextField = "DeliveryOption"
radioButtonSelection.DataBind()
dtrCust.Close()
conCust.Close()
End If
End Sub
</Script>
<html>
<head><title>Radio Button Control Data Binding </title></head>
<body>
<form Runat="Server">
<asp:RadioButtonList ID="radioButtonSelection" Runat="Server" />
<p>
</form>
</body>
</html>
Check Box
Check box controls are stored and retrieved identically to how radio buttons are stored and retrieved. The only difference is that you are using a check box instead of a radio box. The next example shows how to create a check box that uses the customer’s last name as its label. Refer to Chapter 8 for more information about how to incorporate a check box control into your application.
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
If Not IsPostBack Then
Dim conCust As SqlConnection
Dim cmdSelectRows As SqlCommand
Dim dtrCust As SqlDataReader
conCust = New SqlConnection( "Server=localhost;UID=
MyID;PWD=MyPassword;Database=CustomerContact Data")
cmdSelectRows = New SqlCommand("Select custLastName From
custContact", conCust)
conCust.Open()
dtrCust = cmdSelectRows.ExecuteReader()
checkBoxSelection.DataSource = dtrCust
checkBoxSelection.DataTextField = "Customer Last Name"
checkBoxSelection.DataBind()
dtrCust.Close()
conCust.Close()
End If
End Sub
</Script>
<html>
<head><title>Check Box Control Data Binding</title></head>
<body>
<form Runat="Server">
<asp:CheckBoxList ID="checkBoxSelection" Runat="Server" />
</form>
</body>
</html>
Next: List Box >>
More ASP.NET Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 12 of ASP.NET 2.0 DeMYSTiFieD, written by Jim Keogh (McGraw-Hill/Osborne; ISBN: 0072261412). Check it out today at your favorite bookstore. Buy this book now.
|
|