Programming Microsoft ASP.NET 4 - Dino Esposito [229]
Within the delimiters, you can invoke user-defined page methods, static methods, and properties and methods of any other page component. The following code demonstrates a label bound to the name of the currently selected element in a drop-down list control:
Note that if you’re going to use quotes within the expression, you should wrap the expression itself with single quotes. The data-binding expression can accept a minimal set of operators, mostly for concatenating subexpressions. If you need more advanced processing and use external arguments, resort to a user-defined method. The only requirement is that the method be declared as public or protected.
Important
Any data-bound expression you define in the page is evaluated only after DataBind is called. You can call DataBind either on the page object or on the specific control. If you call DataBind on the page object, it will recursively call DataBind on all controls defined in the page. If DataBind is not called, no <%# …%> expressions will ever be evaluated.
Binding in Action
Data-binding expressions are particularly useful to update, in a pure declarative manner, properties of controls that depend on other controls in the same page. For example, suppose you have a drop-down list of colors and a label and that you want the text of the label to reflect the selected color:
Note that in the <%# … %> expression you can use any combination of methods, constants, and properties as long as the final result matches the type of the bound property. Also note that the evaluation of the expression requires a postback and a call to DataBind in the post-back event handler. You set the AutoPostBack property to true just to force a postback when the selection changes in the drop-down list. At the same time, a call to the page’s or label’s DataBind method is required for the refresh to occur. protected void Page_Load(object sender, EventArgs e) { ... DataBind(); } You can bind to expressions virtually any control properties regardless of the type. Note You can use data-binding expressions to set control properties in a declarative manner; you cannot use plain code blocks—that is, <% … %> expressions—without the # symbol for the same purpose. Implementation of Data-Binding Expressions When the page parser takes care of the ASPX source file, it generates a class where each server control has a factory method. The factory method simply maps the tag name to a server-side control class and transforms attributes on the tag into property assignments. In addition, if a data-binding expression is found, the parser adds a handler for the DataBinding event of the control—a Label in this case. Here’s some pseudocode to illustrate the point: private Control __BuildControlToday() { Label __ctrl = new Label(); this.today = __ctrl; __ctrl.ID = "today"; __ctrl.DataBinding += new EventHandler(this.__DataBindToday); return __ctrl; } The handler assigns the data-binding expression verbatim to the property: public void __DataBindToday(object sender, EventArgs e) { Label target; target = (Label) sender; target.Text = Convert.ToString(DateTime.Now); } If the value returned by the data-binding expression doesn’t match the expected type, you generally get a compile error. However, if the expected type is string,
What really happens when a data-binding expression is found in a Web page? How does the ASP.NET runtime process it? Let’s consider the following code: