Controls that Are Datasource Bound

ASP .NET 2.0 provides several groups of databound controls: AdRotor, ListControl(BulletedList, CheckboxList, drop-down list, ListBox, RadioButtonList), CompositeDataBoundControl (DatailsView, FormView, GridView), and HierachicalDataBoundControl(TreeView, Menu). ASP .NET 1.x users are already familiar with ListControls such as RadioButtonList, drop-down list; however, BulletedList is a brand new member of the ListControl family.

BulletedList works as an HTML list. However, it can be directly populated from a backend database. Here is an example of BulletedList:

      Value="">Bullet One
      Value="">Bullet Two
Note:The "~" in the line of BulletImageUrl is the web application root operator and denotes the root of the web site. It can be used to specify a URL in a server control's attribute. BulletedList has several display modes: text, hyperlink, and linkbutton.

DataSource Control

In the old days of ASP, if you wanted populate a list of radio buttons, checkboxes, or HTML select option items, you wrote database connection and queries, and used a "for" or "while" loop to write out the items one by one. In the not-so-old ASP .NET 1.x days, you were saved from some of the coding, especially if you had an IDE like VS .NET. You dragged and dropped a server control to the web form. Then, after some property editing, you sneaked into the code-behind code file and did the same old ASP thing: established the database connection and built a query; then, we declared. Now, this is the source: Let the drop-down list or whatever be married to the datasource.

ASP .NET 2.0 follows the same logic; however, it moves one step further into a higher ground. It gives you a new type of server control called data source controls; these include SqlDataSource, XmlDataSource, AccessDataSource, and so forth. Like other web controls, data source controls can be dragged and dropped onto the web form. The VB 2005 IDE also provides a Configure Data Source wizard.

The ASPX code for the data souce control follows:

<asp:SqlDataSource runat="server"
     ConnectionString="<%$ ConnectionStrings:
                       NorthwindConnectionString %>"
     SelectCommand="SELECT [EmployeeID], [LastName], [FirstName]
                    FROM [Employees]">

That is it. That saves your labor of switching to the code-behind file, and dutifully spits out all the necessary database instructions. Once the datasource is created and configured, you can bind a drop-down list or any databound controls to it.

The following is the code for a dropdown list that displays all the employee names from the Northwind database.

<!--declare a sqldatasource. Modified the selectcommand a little
    so we can employees' full name -->
<asp:SqlDataSource runat="server"
     ConnectionString="<%$ ConnectionStrings:
                       NorthwindConnectionString %>"
     SelectCommand="SELECT [EmployeeID], [LastName] + ' '+
                    [FirstName] as name FROM [Employees]">

<asp:drop-down list ID="ddlEmployees"
     runat=server Width="150px" AutoPostBack=true
     DataSourceID=dsEmployees DataTextField="name"
</asp:drop-down list> 


Of all the databound controls, GridView is the most powerful and versatile one. In ASP .NET 2.0, GridView takes place of the datagrid web control of ASP .NET 1.x, even though datagrid is still supported for backward compatibility. GridView extends and exhances many functionalities of datagrid and makes data display and editing easier and code-efficient. In ASP .NET 1.x, programmers need to write a lot of cusom code for such common database operations as paging and sorting. (Hey, does anyone remember how much more you need to write for paging in classic ASP?) However, The GridView control has a bulit-in sorting functionality. All you have to do is set the AllowSorting attribute to be ture and provide a SortExpression attribute for each each column concerned. Similarily, by simply setting the AllowPaging attribute to true, you accomplish the once-formidable task of paging. GridView can handle editing automatically too.

The aspx code for a GridView that enables sorting and paging:

<asp:SqlDataSource runat="server"
     ConnectionString="<%$ ConnectionStrings:
                       NorthwindConnectionString %>"
     SelectCommand="SELECT [CustomerID], [CompanyName],
                    [ContactName], [ContactTitle], [Address],
                    [City], [Region], [PostalCode], [Country]
                    FROM [Customers]">

<asp:GridView ID="GridView1"
     runat=server AutoGenerateColumns="false"
                  AllowPaging=true AllowSorting=true>
      <asp:BoundField DataField="CustomerID" HeaderText="ID"
           sortexpression="CustomerID" />
      <asp:BoundField DataField="CompanyName"
           sortexpression="CompanyName" /> 
      <asp:BoundField DataField="ContactName"
           sortexpression="ContactName" />
      <asp:BoundField DataField="ContactTitle"
                                 sortexpression="ContactTitle" />
      <asp:BoundField DataField="address" HeaderText="address"
           sortexpression="address" />
      <asp:BoundField DataField="region" HeaderText="region"
           sortexpression="region" />
      <asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
           sortexpression="PostalCode" />
      <asp:BoundField DataField="country" HeaderText="country"
           sortexpression="country" />
   <HeaderStyle BackColor="lightblue" />
   <PagerStyle BackColor="lightblue" HorizontalAlign="Center" />

GridView also enables data editing with little additional code, provided you configure your data source with updated, delete and insert statements.

Here is the ASPX code for a GridView with edit and delete command buttons:

<asp:SqlDataSource runat="server"
     ConnectionString="<%$ ConnectionStrings:
                       NorthwindConnectionString %>"
     SelectCommand="SELECT [CategoryID], [CategoryName],
                    [Description] FROM [Categories]"
     DeleteCommand="Delete from categories
                    where categoryID=@categoryID"
     UpdateCommand="Update categories set CategoryName=@categoryName,
                    where categoryID=@categoryID">
      <asp:Parameter Name=categoryID Type=int16 />
      <asp:Parameter Name=categoryID Type=int16 />
      <asp:Parameter Name=categoryName Type=string />
      <asp:Parameter Name=description Type=string />

<asp:GridView ID="GridView2" runat=server
     datasourceid="SqlDataSource2" AllowPaging=True
   <HeaderStyle BackColor="LightBlue" />
   <PagerStyle BackColor="LightBlue" HorizontalAlign="Center" />
      <asp:BoundField DataField="CategoryID" HeaderText="ID"
      <asp:BoundField DataField="CategoryName" HeaderText="Name"/>
      <asp:BoundField DataField="Description"
                                 HeaderText="Description" />
      <asp:CommandField ButtonType=button ShowEditButton=true
           CausesValidation=false />
      <asp:CommandField ButtonType=Button ShowDeleteButton=true
           CausesValidation=false />

Although there is no support for displaying master-detail views in ASP .NET 1.x, in ASP .NET 2.0, the GridView comes with a pair of complementary view controls: DetailsView and FormView, which enable displaying hierarchical parent-child data tables with little code. Whereas the two view controls are mainly designed for the GridView, they also can be used with other databound controls such as drop-down lists or on their own.

The following is an example of master-detail page consisting of a GridView and a DetailsView. You use two datasources, one for the parent GridView, the other for the child DetailsView.

   <asp:SqlDataSource runat="server" 
      ConnectionString="<%$ ConnectionStrings:
                        NorthwindConnectionString %>"
      SelectCommand="SELECT [CategoryID], [CategoryName],
                     [Description] FROM [Categories]">

   <asp:GridView runat=server ID=GridViewCategory
       datakeynames="categoryid" AutoGenerateColumns=true
       selectedIndex=0 >
         <asp:CommandField ButtonType=button
                 ShowSelectButton=true />
      <SelectedRowStyle BackColor=lightblue />

<td valign=top>
   <asp:SqlDataSource runat="server"
        ConnectionString="<%$ ConnectionStrings:
                          NorthwindConnectionString %>"
        SelectCommand="SELECT [ProductID], [ProductName],
                       [SupplierID], [CategoryID],
                       [QuantityPerUnit], [UnitPrice],
                       [UnitsInStock], [UnitsOnOrder],
                       [ReorderLevel] FROM [Products]
                       where [CategoryID]=@CategoryID">
         <asp:ControlParameter ControlID="GridViewCategory"
              PropertyName="SelectedValue" Type=string/>
<asp:DetailsView ID="DetailsViewProduct" runat=server
DataSourceID="SqlDataSourceProducts" datakeynames="productID"

The DetailsView control has a built-in paging feature. It also allows update, insert, and delete functions.

The Formview control works similarily to the DatailsView Control. However, FormView needs templates instead of a table for data display. Formview enables you to have more control over the layout of the data.

About the Author

Xun Ding

I have been working as a web developer for almost five years. Love various aspects of .NET, love this business of programming. Always want to learn more.


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date