Web 表单错误消息:“这不是 scriptlet.将作为纯文本输出" [英] Web Forms error message: "This is not scriptlet. Will be output as plain text"
问题描述
在我的 ASP .NET Web 窗体中,我有以下声明性代码:
In my ASP .NET Web Forms I have the following declarative code:
<asp:TextBox runat="server" ID="txtbox" CssClass='<%=TEXTBOX_CSS_CLASS%>' />
常量 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";
}
然而,编辑时编译器警告我这不是 scriptlet [原文如此].将输出为纯文本".言归正传,css 类的字面意思是<%=TEXTBOX_CSS_CLASS%>".
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?
推荐答案
您不能使用 <%= ... %>
来设置服务器端控件的属性.内联表达式 <% %>
只能用于aspx 页面或用户控件的顶级文档级别,但不能嵌入服务器控件的标记属性(例如 <asp:Button... Text =<% %> ..>
).
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 =<% %> ..>
).
如果您的 TextBox 位于 DataBound 控件(例如 GridView、ListView ..)内,您可以使用:<%# %>
语法.或者,您可以从代码隐藏或内联服务器脚本中显式调用控件上的 DataBind()
.
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 %>' />
//文件背后的代码
protected void Page_Load(object sender, EventArgs e)
{
txtbox.DataBind();
}
ASP.NET 包括几个内置的表达式生成器,允许您从 web.config
文件中提取自定义应用程序设置和连接字符串信息.示例:
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:
- 资源
- 连接字符串
- 应用设置
因此,如果您想从 web.config
文件的
部分检索名为 className
的应用程序设置,您可以使用以下表达式:
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 %>" />
然而,上面的代码片段并不是从 Appsettings 读取类名的标准.
However, above snippet isn't a standard for reading classnames from Appsettings.
您可以构建和使用您自己的自定义 ExpressionBuilders
或使用代码隐藏:
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" />
这篇关于Web 表单错误消息:“这不是 scriptlet.将作为纯文本输出"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!