Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 531
cbp Avatar asked Sep 23 '13 06:09

cbp


People also ask

What if my form contains errors?

If someone submits your form and then sees a red box with the message “Your form contains errors” they wouldn’t actually know where the errors are located. Instead, avoid making your visitors feel confused by placing the message next to the error itself.

What is this ASP NET Web Forms tutorial series about?

This tutorial series will teach you the basics of building an ASP.NET Web Forms application using ASP.NET 4.5 and Microsoft Visual Studio Express 2013 for Web. A Visual Studio 2013 project with C# source code is available to accompany this tutorial series.

How do I create an error page in a form?

Select the Visual C# -> Web templates group on the left. From the middle list, select Web Form with Master Page ,and name it ErrorPage.aspx. Click Add. Select the Site.Master file as the master page, and then choose OK.

What happens if I request a page that does not exist?

When you request the NoPage.aspx page, which does not exist, the error page will show the simple error message and the detailed error information if additional details are available. However, if the user requested a non-existent page from a remote location, the error page would only show the error message in red.


1 Answers

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" /> 
like image 143
R.C Avatar answered Sep 23 '22 11:09

R.C