The server tag is not well formed. When passing parameters to javascript function

Hi have always trouble with calling JavaScript function with parameters values as values bonded to the gridview. I follow SO Thread But can not used for passing this parameter . Below is the statement that I used in my aspx page.

onblur="return ValidateText(this,'<%# Eval("vehicleId") %>')"

But this gives me parsing error The server tag is not well formed. How should I call this function in design only(not from code behind).

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This works, but seems an HACK because I removed the enclosing quotes (anti-XHtml)

 onblur=<%# String.Format("return ValidateText(this, '{0}')", Eval("VehicleID")) %>

The problem is having " "" " or ' '' ' as part of the string.

This is better done (I wish it is easier in markup), in code-behind, e.g. RowDataBound or ItemDataBound

 control.Attributes["onblur"] = String.Format("return ValidateText(this, '{0}')", rowValue);

Solution 2

I think the problem is the outer-most double quotes ", change that to single quotes '.

onblur='return ValidateText(this,'<%# Eval("vehicleId") %>')'

UPDATE

Try using string.Format() as mentioned in the SO question your are refering..

onblur='<%# System.String.Format("return ValidateText(this, \"{0}\")", Eval("vehicleId")) %>'

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply