为什么<%=%>标记呈现为"&lt;%=%>"?

dan*_*yy1 6 asp.net

我在这样的页面上有一个输入控件:

<input 
    type="button" 
    causesvalidation="false"
    runat="server" 
    id="resetButton"  
    value="Iptal" 
    onclick='return  
    resetForm("<%=projectValidationSummary.ClientID%>");' />
Run Code Online (Sandbox Code Playgroud)

什么时候渲染

<input 
    name="ctl00$ContentPlaceHolder1$EditForm$resetButton" 
    type="button" 
    id="ctl00_ContentPlaceHolder1_EditForm_resetButton" 
    value="Iptal" 
    onclick="return  resetForm(&quot;&lt;%=projectValidationSummary.ClientID%>&quot;);" />
Run Code Online (Sandbox Code Playgroud)

<%=%>在页面上使用标签,但它被渲染为

&quot;&lt;%=%>&quot;
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我为什么会这样吗?

ban*_*ang 4

<%= %> 只能在文字 html 中使用,不能在服务器控件属性上使用。

相反,您应该使用数据绑定 <%# %>,在您的情况下,我认为您正在尝试在客户端触发 javascript 函数,然后您的代码应如下所示:

<asp:button
causesvalidation="false"
runat="server"
id="resetButton"
text="Iptal"
onclientclick='<%# String.Format("return resetForm(\"{0}\");", projectValidationSummary.ClientID) %>' />
Run Code Online (Sandbox Code Playgroud)

在服务器端,您应该使用以下代码绑定属性(可能在 Page.Load 事件中):

if(!this.IsPostBack)
{
  this.resetButton.DataBind();
}
Run Code Online (Sandbox Code Playgroud)