Marthinus Elliott
Marthinus Elliott

Reputation: 67

Encoding and Decoding Strings From Javascript to C# - Not Working

I am having an issue with some string encoding, maybe its a iis issue, or something i am overlooking. Basically, randomly, when users commit changes, the text gets stored with the %20, as if its completely ignoring my decode. It never happens to me on localhost, live, or with any browser; its never consistant, dont even know how to test what is going wrong.

Just gonna paste the basic parts:

Text Control accepting user input

<asp:TextBox TextMode="MultiLine" runat="server" txtActionUpdate"></asp:TextBox>

then

Javascript "escape" and passed to ajax method to be sent to codebehind

var tAction = escape(document.getElementById("txtActionUpdate").value);

then

In the webmethod i decode with the following:

Test = Microsoft.JScript.GlobalObject.unescape(tAction);

(Previously was: Test = HttpUtility.UrlDecode(tAction);)

But none the less both decode options above work perfectly (for me?). But randomly, when users are working on it, it will literally "skip" the decode step and just write it to the DB in its encoded format (ie: this%20is%20a%20Test%20string), as if i never had the code at all.

We have multiple customers (separate URL's) but one code base. During publish, i delete first then publish, clear app pools, get the users to clear browser cache (hate the latter actually), but this never seems to remain consistent. I have a feeling this is more some "caching" or "server settings" issue rather than a code problem... making me look bad! ;) lol. Anyone got some hunches?

Upvotes: 0

Views: 2258

Answers (2)

Lucero
Lucero

Reputation: 60190

My guess is that somewhere on the client the data is encoded twice, which then leads to the effect you're seeing.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074485

I wouldn't use escape for this (or anything else). I'd use encodeURIComponent, then decode with HttpUtility.UrlDecode (I think; MSDN is down right now, apparently).

Upvotes: 1

Related Questions