Binding Data to Controls


A key advantage in using the rich set of ASP.NET Web Server controls is that developers can data bind a number of controls to server-side data sources. The data itself can come from various kinds of data sources ”databases using ADO.NET, Web services, XML, and so on. The unique combination of data binding and a set of enriched controls is really what makes ASP.NET a pleasure to work with in its capability to rapidly develop and deploy rich applications. For instance, examine the following code snippet, which uses a simple data access class called Employees in the namespace hks to retrieve employee contact information. The Employees class itself is pretty straightforward; utilizing the ADO.NET class library to connect with a SQL Server database, executing a simple Select SQL statement, and returning a DataSet. This example also illustrates usage of an Import directive that allows ASP.NET to use other classes from the .NET Framework class library and even user -defined classes.

 
 using System; using System.Data; using System.Data.SqlClient; namespace hks {        public class Employees       {                public Employees()                {                }                public DataSet GetEmployees()                {                       String conStr = "Data Source=POWER2003;"                            +"Initial Catalog=Book;User ID=sa;Password=sa";                       SqlConnection cn = new SqlConnection(conStr);                       cn.Open();                       String strSelectSql = "select * From Employees";                       SqlDataAdapter cmdSelect =                             new SqlDataAdapter(strSelectSql, cn);                       DataSet ds = new DataSet();                       cmdSelect.Fill(ds,"Employees");                       cn.Close();                       return ds;                }        } } 

Next, take a look at the code required to use the DataSet to dynamically display employee information in a Web page.

 
 <%@ Page Language="C#" %> <%@ Import namespace="System.Data" %> <%@ Import namespace="hks" %> <html> <head>  <script runat="server">        void Page_Load(Object sender, EventArgs e) {             Employees emp = new Employees();             DataSet ds = emp.GetEmployees();             grid.DataSource = ds;             grid.DataBind();        }  </script> </head> <body>        <asp:DataGrid id="grid" runat="server"/> </body> </html> 

If you have developed data-enabled Web applications before, you are probably surprised to see the simplicity of the code to do a complex task. As you can see, just by specifying a DataGrid control and data-binding to a DataSet received from calling the Employees class's GetEmployees() method, you are able to develop almost a fully featured, yet elegant, Web application (see Figure 8.6).

Figure 8.6. Using data binding with DataGrid control.

At this time, you are probably thinking, "I can only show some information in an HTML table; what if I want to drill down to a detail page ”or what if I want to provide a separate edit page?" Take a look at the code snippet that follows . With the simple addition of an <asp:HyperlLinkColumn> tag, you can do so (see Figure 8.7).

 
 <%@ Page Language="C#" %> <%@ Import namespace="System.Data" %> <%@ Import namespace="hks" %> <html> <head>  <script runat="server">       void Page_Load(Object sender, EventArgs e) {              // same as before       }  </script> </head> <body>        <asp:DataGrid id="grid" runat="server"            BorderColor="Black"            GridLines="Both"            HeaderStyle-BackColor="#ffff00">          <Columns>           <asp:HyperLinkColumn DataNavigateUrlField="Id"            DataNavigateUrlFormatString="ViewEmployee.aspx?Id={0}"            Text="More Info"/>           </Columns>        </asp:DataGrid> </body> </html> 
Figure 8.7. Master-details relationship between pages using HyperLinkColumn.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net