How can I use property expressions in ASP.NET ASPX server tags?
Posted: (EET/GMT+2)
I was recently working on a slightly older web application built using ASP.NET and using the WebForms technology (ASPX files). I wanted to add tooltips to numerous input text boxes, all with the same text strings.
The traditional way to do this would be to use something like this:
<%= MyToolTipText %>
...where "MyTooltipText" is a property defined in the code-behind file. This solution works great for any plain HTML tag (element) in your ASPX page, but you cannot use this syntax for attributes inside ASP.NET tags. For example, if you wanted to specify the "tooltip" attribute for the TextBox control, you cannot use the above server tag with the equal sign.
Instead, you could use so-called data-binding expressions, like this:
<%# MyToolTipText %>
The hash sign # instructs ASP.NET to data-bind the given property value. However, there's one extra step needed to make this work: you must call the "DataBind()" method in your Page_Load event. This method is part of the ASP.NET Page class, and thus always available.
Hope this helps!