Controls that Are Datasource Bound

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read