The next section will give you a good idea of how ASP.NET data binding works and how your code can take advantage of the various binding mechanisms available. You will see examples of simple binding, complex binding, and the methods available in controls and page classes that facilitate data binding.
Web Forms data binding is much different from the dynamic, bi-directional update model that you saw in Chapter 30, "Windows Forms Data Binding." The ASP.NET model is specifically designed with the notion that the vast majority of all data-related tasks involve retrieving data and rendering it in some fashion, whether it is simple output or an extremely complex grid. As a result, ASP.NET data binding is read-only. When you bind a control in ASP.NET, it does not have the capability to dynamically modify the original data source.
There are several reasons for that. The first and foremost reason is that for most data modifications to take place, a round-trip must occur and the client browser must re-request the page. On a re-request, the interface control has no way of knowing whether the data source it was originally bound to will exist after the round trip, or if the data will remain the same. This is why most data-bound controls allow their bound data to be stored in ViewState to appear again after a round trip to the server.
There are several aspects to ASP.NET data binding. The first is binding the data to a control, and the second is rendering the bound data. You can render the bound data in any number of ways. The next few sections show you some of the ways in which data can be rendered for a data-bound control.
Those of you familiar with classic ASP applications might think that the <%# %> syntax looks very familiar. It is similar in purpose, but you need to make sure that you don't confuse the two because doing so could cause your application to function improperly.
Whereas in ASP (and ASP.NET), the <%= %> syntax causes whatever is inside the brackets to be evaluated at render time, the <%# %> brackets unique to ASP.NET are evaluated only during binding. As you will see later in this section, the page and each bindable control on the page have a DataBind() method. The expressions contained within the data binding brackets (<%# %>) are evaluated only when the control's DataBind method is invoked. You will see how to use this syntax shortly.
Simple data binding refers to the situation in which you bind a single value to a property of a control. In addition to binding to the property of a control, you can also simply insert directly into the document the result of a binding operation. Simple data binding is accomplished via the bind evaluation brackets (<%# %>).
Listing 31.1 shows the ASP.NET page that illustrates some very simple data-binding techniques. Listing 31.2 shows the code-behind C# code that provides the background data and method calls that are invoked by the controls.
<%@ Page <HTML> <HEAD> <title>SimpleBinding</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content=""> </HEAD> <body> <form method="post" runat="server"> <asp:TextBox Runat=server</asp:TextBox><br> <asp:Panel Height="<%# height %>" Width="<%# width %>" BorderColor="<%# Color %>" Runat=server BorderStyle=Solid> <%# GetPanelText() %> </asp:Panel> </form> </body> </HTML>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebFormsBinding { public class SimpleBinding : System.Web.UI.Page { public int width = 30; public int height= 30; public Color Color = Color.Red; private void Page_Load(object sender, System.EventArgs e) { // if you take this code out, none of the bound controls will be bound // and no <%# %> expressions will be evaluated DataBind(); } public string myFunction() { return "This came from a bound function call"; } public string GetPanelText() { return "This is inside a panel"; } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
A couple of different things are going on in this sample. The value of the text box is bound (remember this is read-only) to the result of the function myFunction. A panel's various properties are controlled by being bound to some public fields on the page class, such as the height, width, and color. In addition, the text that appears inside the panel is dictated by the GetPanelText() method. It can't be stressed enough that data binding in ASP.NET is a one-time deal. When the page loads, you choose when the DataBind() method is called and for which controls. That method then supplies data to controls based on the code you've written. The controls may maintain their own state between page views, but the data in the control is never automatically written back to the original sourcethat is something you will have to write manually.
Simple data binding deals with simple scalar values, such as strings and integers. Complex data binding deals with list-type data. In fact, any data type that implements the IEnumerable interface can be bound as a list-type to any of the server controls that support multiple rows of data, such as the DataGrid, DataList, and Repeater. You will see these controls demonstrated shortly.
As you saw in the first sample on simple data binding, there is a method that you can invoke on the page class that will perform data binding for every control on the page in a cascading fashion. If there are containers of other controls on the page when the DataBind() method is called, those controls will be data-bound at the time. Each bindable control in the entire library of ASP.NET server controls has a DataBind() method.
Unlike WinForms, where you simply set the data source and everything magically appears, ASP.NET requires you to invoke the DataBind() method for any data binding to occur.