ASP.NET
  Home arrow ASP.NET arrow Page 4 - Binding Data to Controls
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

Binding Data to Controls
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-04-27

    Table of Contents:
  • Binding Data to Controls
  • The Repeater Control
  • A Closer Look at Templates
  • Drop-Down List

  • 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


    Binding Data to Controls - Drop-Down List


    (Page 4 of 4 )

    Data can also be bounded to the DropDownList control by assigning the column name to the DataTextField property of the DropDownList control in the Page_Load method. This is illustrated in the next example, where we load the last name of customers into a drop-down list when the page is loaded.

    Notice that we connect to the database and execute the query in the Page_Load subroutine much as we did in the previous example, with one difference. The database connection is made within an If statement that evaluates the status of the IsPostBack property.

    A postback occurs when the page calls itself. A visitor loads a page for the first time by entering the page’s URL into the browser address box or by clicking a hyperlink contained on a different page. This is not a postback. However, once the page is displayed, the page can request itself. This is a postback.


    Figure 12-1.  The last names shown in the DropDownList control are from the database.

    If the page is a postback, then the IsPostBack property is true; otherwise, the IsPostBack property is false. Data connection and data binding occur only when the page isn’t a postback. Therefore, we need to test the value of the IsPostBack property before connecting to the DBMS and binding the data. We do this by reversing the logic of the IsPostBack property. That is, if the IsPostBack property is false (the page is loaded the first time), then we make the condition expression true so that statements within the If statement (connect to the DBMS and bind the data) are executed.

    The web page itself is different than the previous example because we created a form that contains the DropDownList. The DropDownList control is populated with the last name of customers from the Customers table (Figure 12-1).

    There would be other controls in a real-world application such as a button that when selected causes the selected customer last name to be processed (see Chapter 8).

    <%@ 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")
      
    conCust.Open()
      
    cmdSelectRows = New SqlCommand( "Select cistLastName From
    custContact", conCust)
      
    dtrCust = cmdSelectRows.ExecuteReader()
       deleteCust.DataSource = dtrCust
         
    deleteCust.DataTextField = "custLastName"
          
    deleteCust.DataBind()
          
    dtrCust.Close()
          
    conCust.Close()
        End If
    End Sub
    </Script>
    <html>
    <head><title>Drop-Down List Control Data Binding</title></head>
    <body>
      
    <form Runat="Server">
         
    <asp:DropDownList ID="deleteCust" Runat="Server" />
      
    </form>
    </body>
    </html>

    Please be sure to check back next week for the conclusion of this article!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - 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

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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