解析服务器端标记时出错

时间:2021-10-20 16:14:46

Invalid expression term '<'

无效的表达式术语'<'

<asp:TextBox ID="txtPassword" runat="server" 
            Width="180px" TextMode="Password" 
            OnTextChanged="CheckPasswordStrength(<%= txtPassword.ClientID.ToString() %>,<%= lblMessage.ClientID.ToString() %>)"/>

If i writes this code like the following then error occurs An unhandled exception has occured. Server tags cannot contain <% %> constructs

如果我像下面这样写这个代码然后发生错误一个未处理的异常发生。服务器标记不能包含<%%>结构

  <asp:TextBox ID="txtPassword" runat="server" 
                Width="180px" TextMode="Password" 
                OnTextChanged="CheckPasswordStrength("<%= txtPassword.ClientID.ToString() %>","<%= lblMessage.ClientID.ToString() %>")"/>

When I m using this code at .cs file then every thing is working fine.

当我在.cs文件中使用此代码时,每件事情都运行良好。

 protected void Page_Load(object sender, EventArgs e)
    {
        txtPassword.Attributes.Add("onKeyUp", "PasswordCheck("+txtPassword.ClientID.ToString()+")");
        txtPrimaryEmail.Attributes.Add("onKeyUp", "EmailChecker("+txtPrimaryEmail.ClientID.ToString()+")");
    }

3 个解决方案

#1


4  

There are a couple things going on with this.. You can't include parameters in your server-side event, and you can't use <%= in a server control.

这有几件事情。你不能在服务器端事件中包含参数,也不能在服务器控件中使用<%=。

Are you meaning to fire a JavaScript event?

你是不是想发动一个JavaScript事件?

If you're meaning to fire a JavaScript event, do one of three things:

如果您要触发JavaScript事件,请执行以下三项操作之一:

1) Use a databinding expression (<%# Control.ClientID %>) - This requires that somewhere within the life-cycle DataBind() is being called on your control.

1)使用数据绑定表达式(<%#Control.ClientID%>) - 这要求在控件上调用生命周期DataBind()中的某个位置。

2) Assign the event in the code-behind, using Control.Attributes.Add("javascriptevent", "DoStuff(x, y)")

2)使用Control.Attributes.Add(“javascriptevent”,“DoStuff(x,y)”)在代码隐藏中分配事件

3) You can use <%= %> in your client script, e.g.

3)您可以在客户端脚本中使用<%=%>,例如

function MyJavaScriptEventHandler()
{
   var textbox = document.getElementById('<%= MyASPTextBox.ClientID %>');
   alert(textbox.value);
}

#2


0  

I don't think you can include parameters in a Server Event. You will need to reference those controls from the Code-Behind.

我认为您不能在服务器事件中包含参数。您需要从Code-Behind引用这些控件。

#3


0  

Yeah. Server controls cannot contain <% (evaluation for those tags occurs after the server controls - so those tags are considered part of the server control and it fails parsing).

是啊。服务器控件不能包含<%(在服务器控制之后对这些标记进行评估 - 因此这些标记被视为服务器控件的一部分,并且无法解析)。

You might want to add the ontextchanged attribute in your code-behind. You could also use JavaScript.

您可能希望在代码隐藏中添加ontextchanged属性。你也可以使用JavaScript。

#1


4  

There are a couple things going on with this.. You can't include parameters in your server-side event, and you can't use <%= in a server control.

这有几件事情。你不能在服务器端事件中包含参数,也不能在服务器控件中使用<%=。

Are you meaning to fire a JavaScript event?

你是不是想发动一个JavaScript事件?

If you're meaning to fire a JavaScript event, do one of three things:

如果您要触发JavaScript事件,请执行以下三项操作之一:

1) Use a databinding expression (<%# Control.ClientID %>) - This requires that somewhere within the life-cycle DataBind() is being called on your control.

1)使用数据绑定表达式(<%#Control.ClientID%>) - 这要求在控件上调用生命周期DataBind()中的某个位置。

2) Assign the event in the code-behind, using Control.Attributes.Add("javascriptevent", "DoStuff(x, y)")

2)使用Control.Attributes.Add(“javascriptevent”,“DoStuff(x,y)”)在代码隐藏中分配事件

3) You can use <%= %> in your client script, e.g.

3)您可以在客户端脚本中使用<%=%>,例如

function MyJavaScriptEventHandler()
{
   var textbox = document.getElementById('<%= MyASPTextBox.ClientID %>');
   alert(textbox.value);
}

#2


0  

I don't think you can include parameters in a Server Event. You will need to reference those controls from the Code-Behind.

我认为您不能在服务器事件中包含参数。您需要从Code-Behind引用这些控件。

#3


0  

Yeah. Server controls cannot contain <% (evaluation for those tags occurs after the server controls - so those tags are considered part of the server control and it fails parsing).

是啊。服务器控件不能包含<%(在服务器控制之后对这些标记进行评估 - 因此这些标记被视为服务器控件的一部分,并且无法解析)。

You might want to add the ontextchanged attribute in your code-behind. You could also use JavaScript.

您可能希望在代码隐藏中添加ontextchanged属性。你也可以使用JavaScript。