ASP.NET
  Home arrow ASP.NET arrow Learning More About Binding Data to Contro...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Learning More About Binding Data to Controls
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-05-04

    Table of Contents:
  • Learning More About Binding Data to Controls
  • List Box
  • Hyperlinks
  • Quiz

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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>

    More ASP.NET Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "ASP.NET 2.0 DeMYSTiFieD," published by...
     

    Buy this book now. 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.

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT