0

I have the following function in JavaScript, using which I call a PageMethod in ASP.Net.

The trouble is when I pass a string like xy\23 or 2% for the parameter searchStringVal, then I get a JavaScript error. Anyway I can handle this sort of problem? This parameter is finally passed to a static server-side method in my ASP.Net page, and this further compounds this problem. So I am looking for the most efficient approach to use in this situation.

One solution could be to encode strings in JavaScript, but I need to be able to decode it in ASP.Net C# code, without losing the original value of string.

function CallPageMethod(startRowIndex, searchStringVal) {        
    if (newVal != searchStringVal) {
        //document.getElementById('divMessage').style.visibility = "hidden";
        return;
    }

    start = new Date();
    PageMethods.GetData(startRowIndex, tableView.get_pageSize(), searchStringVal, updateGrid, onError, searchStringVal);
}

LATEST UPDATE: My question was answered by two different persons. How can I label both as answers? I ended up using Vinay's recommended approach of encoding in Javascript and then unecoding in C# using Uri.UnescapeDataString method.

2
  • Its a javascript error with a yellow exclamation that appears at bottom left of the browser window and when I click it says 'Unterminated String Constant'. I specified a value of 2xy\ Commented Aug 18, 2012 at 6:51
  • Sunil [Refer This Link][1] Hope You are Looking for this. [1]: stackoverflow.com/questions/4613430/… Commented Aug 18, 2012 at 7:01

1 Answer 1

2

If you're getting an unterminated string constant, then it has to do with how you're entering the string.

Typing "2xy\" is interpreted as a 2xy, and then an escaped double-quote. The string is then underminated.

Try "2xy\\" to enter the string 2xy\ - this uses the backslash to escape the second backslash, and will be recognized as the string you're trying to enter.

Sign up to request clarification or add additional context in comments.

9 Comments

How would I take care of something like 2%, as that also gives a javascript error.
What error does that give? "2%" shouldn't give an unterminated string constant error.
I cannot see a javascript error but there seems to be some error as the page is not responding. This is when I input 2%.
Sorry - I don't think I'll be able to help track the 2% one down unless you have some more information about what's going on. Maybe try putting a breakpoint there, and stepping through to see what's happening?
Yes I will try and let you know later. Thanks for now for your prompt help.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.