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.