Shantanu Gupta
Shantanu Gupta

Reputation: 21108

Error in resolving server side tag

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.

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

Upvotes: 5

Views: 637

Answers (3)

Gabriel McAdams
Gabriel McAdams

Reputation: 58301

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.

Upvotes: 0

zincorp
zincorp

Reputation: 3282

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?

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

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

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

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

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

Upvotes: 4

Mike Cole
Mike Cole

Reputation: 14733

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

Upvotes: 0

Related Questions