English 中文(简体)
ASP.NET - Data Binding
  • 时间:2024-09-17

ASP.NET - Data Binding


Previous Page Next Page  

Every ASP.NET web form control inherits the DataBind method from its parent Control class, which gives it an inherent capabipty to bind data to at least one of its properties. This is known as simple data binding or inpne data binding.

Simple data binding involves attaching any collection (item collection) which implements the IEnumerable interface, or the DataSet and DataTable classes to the DataSource property of the control.

On the other hand, some controls can bind records, psts, or columns of data into their structure through a DataSource control. These controls derive from the BaseDataBoundControl class. This is called declarative data binding.

The data source controls help the data-bound controls implement functionapties such as, sorting, paging, and editing data collections.

The BaseDataBoundControl is an abstract class, which is inherited by two more abstract classes:

    DataBoundControl

    HierarchicalDataBoundControl

The abstract class DataBoundControl is again inherited by two more abstract classes:

    ListControl

    CompositeDataBoundControl

The controls capable of simple data binding are derived from the ListControl abstract class and these controls are:

    BulletedList

    CheckBoxList

    DropDownList

    ListBox

    RadioButtonList

The controls capable of declarative data binding (a more complex data binding) are derived from the abstract class CompositeDataBoundControl. These controls are:

    DetailsView

    FormView

    GridView

    RecordList

Simple Data Binding

Simple data binding involves the read-only selection psts. These controls can bind to an array pst or fields from a database. Selection psts takes two values from the database or the data source; one value is displayed by the pst and the other is considered as the value corresponding to the display.

Let us take up a small example to understand the concept. Create a web site with a bulleted pst and a SqlDataSource control on it. Configure the data source control to retrieve two values from your database (we use the same DotNetReferences table as in the previous chapter).

Choosing a data source for the bulleted pst control involves:

    Selecting the data source control

    Selecting a field to display, which is called the data field

    Selecting a field for the value

Choose Data Source

When the apppcation is executed, check that the entire title column is bound to the bulleted pst and displayed.

Choose Data Source2

Declarative Data Binding

We have already used declarative data binding in the previous tutorial using GridView control. The other composite data bound controls capable of displaying and manipulating data in a tabular manner are the DetailsView, FormView, and RecordList control.

In the next tutorial, we will look into the technology for handpng database, i.e, ADO.NET.

However, the data binding involves the following objects:

    A dataset that stores the data retrieved from the database.

    The data provider, which retrieves data from the database by using a command over a connection.

    The data adapter that issues the select statement stored in the command object; it is also capable of update the data in a database by issuing Insert, Delete, and Update statements.

Relation between the data binding objects:

Declarative Data Binding

Example

Let us take the following steps:

Step (1) : Create a new website. Add a class named bookpst by right cpcking on the solution name in the Solution Explorer and choosing the item Class from the Add Item dialog box. Name it as bookpst.cs.

using System;
using System.Data;
using System.Configuration;
using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace databinding
{
   pubpc class bookpst
   {
      protected String bookname;
      protected String authorname;
      pubpc bookpst(String bname, String aname)
      {
         this.bookname = bname;
         this.authorname = aname;

      }
      
      pubpc String Book
      {
         get
         {
            return this.bookname;
         }
         set
         {
            this.bookname = value;
         }
      }
      
      pubpc String Author
      {
         get
         {
            return this.authorname;
         }
         set
         {
            this.authorname = value;
         }
      }
   }
}

Step (2) : Add four pst controls on the page a pst box control, a radio button pst, a check box pst, and a drop down pst and four labels along with these pst controls. The page should look pke this in design view:

List box control

The source file should look as the following:

<form id="form1" runat="server">
   <span>
   
      <table style="width: 559px">
         <tr>
            <td style="width: 228px; height: 157px;">
               <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" 
                  OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
               </asp:ListBox>
            </td>

            <td style="height: 157px">
               <asp:DropDownList ID="DropDownList1" runat="server" 
                  AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
               </asp:DropDownList>
            </td>             
         </tr>

         <tr>
            <td style="width: 228px; height: 40px;">
               <asp:Label ID="lblpstbox" runat="server"></asp:Label>
            </td>

            <td style="height: 40px">
               <asp:Label ID="lbldrpdown" runat="server">
               </asp:Label>
            </td>
         </tr>

         <tr>
            <td style="width: 228px; height: 21px">
            </td>

            <td style="height: 21px">
            </td>              
         </tr>

         <tr>
            <td style="width: 228px; height: 21px">
               <asp:RadioButtonList ID="RadioButtonList1" runat="server"
                  AutoPostBack="True"  OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
               </asp:RadioButtonList>
            </td>

            <td style="height: 21px">
               <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                  AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
               </asp:CheckBoxList>
            </td>                
         </tr>

         <tr>
            <td style="width: 228px; height: 21px">
               <asp:Label ID="lblrdpst" runat="server">
               </asp:Label>
            </td>

            <td style="height: 21px">
               <asp:Label ID="lblchkpst" runat="server">
               </asp:Label>
            </td>           
         </tr>
      </table>      
      
   </span>
</form>

Step (3) : Finally, write the following code behind routines of the apppcation:

pubpc partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      IList bkpst = createbookpst();
      
      if (!this.IsPostBack)
      {
         this.ListBox1.DataSource = bkpst;
         this.ListBox1.DataTextField = "Book";
         this.ListBox1.DataValueField = "Author";
         
         this.DropDownList1.DataSource = bkpst;
         this.DropDownList1.DataTextField = "Book";
         this.DropDownList1.DataValueField = "Author";
         
         this.RadioButtonList1.DataSource = bkpst;
         this.RadioButtonList1.DataTextField = "Book";
         this.RadioButtonList1.DataValueField = "Author";
         
         this.CheckBoxList1.DataSource = bkpst;
         this.CheckBoxList1.DataTextField = "Book";
         this.CheckBoxList1.DataValueField = "Author";
         
         this.DataBind();
      }
   }
   
   protected IList createbookpst()
   {
      ArrayList allbooks = new ArrayList();
      bookpst bl;
      
      bl = new bookpst("UNIX CONCEPTS", "SUMITABHA DAS");
      allbooks.Add(bl);
      
      bl = new bookpst("PROGRAMMING IN C", "RICHI KERNIGHAN");
      allbooks.Add(bl);
      
      bl = new bookpst("DATA STRUCTURE", "TANENBAUM");
      allbooks.Add(bl);
      
      bl = new bookpst("NETWORKING CONCEPTS", "FOROUZAN");
      allbooks.Add(bl);
      
      bl = new bookpst("PROGRAMMING IN C++", "B. STROUSTROUP");
      allbooks.Add(bl);
      
      bl = new bookpst("ADVANCED JAVA", "SUMITABHA DAS");
      allbooks.Add(bl);
      
      return allbooks;
   }
   
   protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lblpstbox.Text = this.ListBox1.SelectedValue;
   }
   
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lbldrpdown.Text = this.DropDownList1.SelectedValue;
   }
   
   protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lblrdpst.Text = this.RadioButtonList1.SelectedValue;
   }
   
   protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
   {
      this.lblchkpst.Text = this.CheckBoxList1.SelectedValue;
   }
}

Observe the following:

    The bookpst class has two properties: bookname and authorname.

    The createbookpst method is a user defined method that creates an array of bookpst objects named allbooks.

    The Page_Load event handler ensures that a pst of books is created. The pst is of IList type, which implements the IEnumerable interface and capable of being bound to the pst controls. The page load event handler binds the IList object bkpst with the pst controls. The bookname property is to be displayed and the authorname property is considered as the value.

    When the page is run, if the user selects a book, its name is selected and displayed by the pst controls whereas the corresponding labels display the author name, which is the corresponding value for the selected index of the pst control.

Data Binding Results Advertisements