In my ASP .NET Web Forms I have the following declarative code:
\' />
You cannot use <%= ... %>
to set properties of server-side controls.
Inline expressions <% %>
can only be used at
aspx page or user control's top document level, but can not be embeded in
server control's tag attribute (such as <asp:Button... Text =<% %> ..>
).
If your TextBox is inside a DataBound controls such as GridView, ListView .. you can use: <%# %>
syntax. OR you can call explicitly DataBind()
on the control from code-behind or inline server script.
<asp:TextBox runat="server" ID="txtbox" class='<%# TEXTBOX_CSS_CLASS %>' />
// code Behind file
protected void Page_Load(object sender, EventArgs e)
{
txtbox.DataBind();
}
ASP.NET includes few built-in expression builders that allows you to extract custom application settings and connection string information from the web.config
file. Example:
So, if you want to retrieve an application setting named className
from the <appSettings>
portion of the web.config
file, you can use the following expression:
<asp:TextBox runat="server" Text="<%$ AppSettings:className %>" />
However, above snippet isn't a standard for reading classnames from Appsettings.
You can build and use either your own Custom ExpressionBuilders
or Use code behind as:
txtbox.CssClass = TEXTBOX_CSS_CLASS;
Check this link on building Custom Expression builders. Once you build your custom Expression you can display value like:
<asp:TextBox Text="<%$ SetValue:SomeParamName %>"
ID="setting"
runat="server" />
This will work.
Mark up
<asp:TextBox runat="server" ID="txtbox" class='<%# TEXTBOX_CSS_CLASS %>' />
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtbox.DataBind();
}
}
But its a lot cleaner to access the CssClass
property of the asp:TextBox
on Page_Load
The problem is that you can't mix runat=server
controls with <%= .. %>
code blocks. The correct way would be to use code behind: txtbox.CssClass = TEXTBOX_CSS_CLASS;
.