Welcome to the twentieth chapter of the FREE online programming course:
A Beginner's Guide to Asp.Net Programming for Delphi developers.
Using <%# binding_expression %> in ASP.NET
In the "Introduction to Data Binding in Delphi ASP.NET Applications" chapter, we've explored one approach to ASP.NET data binding ideally used when populating list oriented web controls (ListBox, DropDownList, etc.).
As you might guess, ASP.NET data binding offers much more flexibility. What if you want to perform binding on an individual control property? How about binding "plain" HTML?
Throughout this chapter, you'll learn about the following:
The <%# binding_expression %> magic
Using binding expression
Binding individual control properties, binding plain HTML
Understanding data-binding expressions
While binding ListControl descendants involves setting DataTextField, DataValueField and some other properties, data binding individual control properties is implemented by using a special data binding expression format. Binding expressions are entered directly in the HTML of a Web Forms page (using the "ASPX" view). Biding expressions take the following form:
<%# binding_expression %>
Data binding expression forms a "link" between visual elements and the data they display.
Biding expressions can supply values for a control property, as in (where "binding_expression" is any valid expression):
<asp:Button
text="<%# binding_expression %>"
id="Button1"
runat="server"
enabled="<%# another_binding_expression %>">
</asp:Button>
|
Binding expressions can also provide dynamic data to "static" HTML, as in (where "GetDayOfWeek" is a public function defined in the code-behind class):
Today is <%# GetDayOfWeek %> and we
are learning ASP.NET with Delphi!
|
You can use binding expression to provide dynamic title for a page, as in (where "PageTitle" is a constant defined in the code-behind class):
<html>
<head>
<title><%# PageTitle %></title>
</head>
...
<body>
...
|
The "binding_expression" can refer to any valid source of data: public constant, property or a method of the Web Form's class or a valid C# expression (since the code for the ASPX page is in C#).
Data-Binding Expression example
Let's see some binding expressions in action...
Drop a Button web server control on an empty web form, add some "free" HTML text, make it all look like:
<html>
<head>
<title><%# PageTitle %></title>
</head>
<body>
<form runat="server">
Today is <%# GetDayOfWeek() %>,
current time is <%# DateTime.Now.ToString() %>
<asp:Button id="Button1" runat="server" text="Hit me!"
enabled="<%# ButtonEnabledState() %>"></asp:Button>
</form>
</body>
</html>
|
Run the page ... uops! Binding expressions failed to return any data ?!?!
Data-binding expressions must be resolved at run time in order to provide values that controls can bind to. In other words, to actually get some values displayed, just call Page.DataBind. This will evaluate any <%# %> expressions within the page.
A simple line of code in the Page_Load will do the trick:
procedure TwfBindExpressions.Page_Load(
sender: System.Object;
e: System.EventArgs);
begin
DataBind;
end;
|
Here's the code driving this Web Form page (TwfBindExpressions, "irrelevant" code omitted):
type
TwfBindExpressions = class(System.Web.UI.Page)
...
public
const PageTitle = 'Learning Binding Expressions';
function GetDayOfWeek : string;
function ButtonEnabledState : boolean;
end;
implementation
...
procedure TwfBindExpressions.Page_Load(
sender: System.Object;
e: System.EventArgs);
begin
DataBind;
end;
function TwfBindExpressions.GetDayOfWeek: string;
begin
result := Convert.ToString(DateTime.Now.DayOfWeek);
end;
function TwfBindExpressions.ButtonEnabledState: boolean;
begin
result := NOT Page.IsPostback;
end;
|
Note: do you know what will happen to Button1's enabled property when you hit it and the page "gets back" from a postback? Yes, it will be disabled! Need a "click_me_only_once button" - there you have it.
To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developers
That's it for today. In the next chapter we'll continue exploring the topic of data binding in ASP.NET.
If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.
A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >>
>>
An introduction to Data-Bound ASP.NET List Controls