Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

asp.net - Web Forms error message: "This is not scriptlet. Will be output as plain text"

In my ASP .NET Web Forms I have the following declarative code:

<asp:TextBox runat="server" ID="txtbox" CssClass='<%=TEXTBOX_CSS_CLASS%>' />

The constant TEXTBOX_CSS_CLASS is defined in a base class that the page's code-behind class inherits from:

public class MyPageBase : Page
{
    protected internal const string TEXTBOX_CSS_CLASS = "myClass";
}

The edit-time compiler however warns me that "This is not scriptlet [sic]. Will output as plain text". True to its word, the css class is rendered as literally "<%=TEXTBOX_CSS_CLASS%>".

What does this error message mean and is there a workaround so I can still use a constant in a base class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  • Resources
  • ConnectionStrings
  • AppSettings

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...