RachitSharma
RachitSharma

Reputation: 589

How to pass a querystring with javascript and use it in codebehind page in asp.net?

Currently I am opening a page in dialog box as shown below using JavaScript. I am passing a query string by encrypting it to the list page .

function Popup() {

        if (document.getElementById("<%= Amount.ClientID %>").value != "") {
            var xorKey = 13;
            var Obj = window;
            var id = document.getElementById("<%= id.ClientID %>").value + "-" + document.getElementById("<%= Amount.ClientID %>").value;

            var result = "";
            for (i = 0; i < id.length; ++i) {
                param += String.fromCharCode(xorKey ^ id.charCodeAt(i));
            }

            window.showModalDialog("list.aspx?id=" + param, Obj, "dialogWidth:800px; dialogHeight:500px; dialogLeft:252px; dialogTop:120px; center:yes");
        }

    } 

Now on code behind page I am using this code :

string id = Request.QueryString[0].ToString();
            StringBuilder inSb = new StringBuilder(id);
            StringBuilder outSb = new StringBuilder(id.Length);
            char c;
            for (int i = 0; i < id.Length; i++)
            {
                c = inSb[i];
                c = (char)(c ^ 13); /// remember to use the same XORkey value you used in javascript
                outSb.Append(c);
            }
            string[] idvalue = outSb.ToString().Split('-');
            id = idvalue[0].ToString();

Now when using the Querystring[0] I am only getting the pre decimal values like if the value I type in textbox is 13.33, then I am on the list page getting only 13. Can anybody help me?

Thank you.

Upvotes: 1

Views: 10970

Answers (2)

Haseeb Asif
Haseeb Asif

Reputation: 1786

Use escape on the param variable as follows

window.showModalDialog("list.aspx?id=" + escape(param), Obj, "dialogWidth:800px; dialogHeight:500px; dialogLeft:252px; dialogTop:120px; center:yes");

or encodeURI

window.showModalDialog(encodeURI("list.aspx?id=" + param), Obj, "dialogWidth:800px; dialogHeight:500px; dialogLeft:252px; dialogTop:120px; center:yes");

Upvotes: 3

pbjork
pbjork

Reputation: 616

Use encodeURIComponent() to encode your url before sending it to the server.

http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

Edit: Added link to source.

Upvotes: 0

Related Questions