javascript 如何使用javascript传递查询字符串并在asp.net的代码隐藏页面中使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23624484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:14:07  来源:igfitidea点击:

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

javascriptasp.netquery-string

提问by RachitSharma

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 .

目前我正在使用 JavaScript 在对话框中打开一个页面,如下所示。我通过将查询字符串加密到列表页面来传递它。

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?

现在,当使用 时,Querystring[0]我只获取前十进制值,就像我在文本框中键入的值是 一样13.33,然后我在列表页面上只获取13. 有谁能够帮我?

Thank you.

谢谢你。

回答by Haseeb Asif

Use escape on the param variable as follows

对 param 变量使用转义如下

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

or encodeURI

或编码URI

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

回答by pbjork

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

使用 encodeURIComponent() 在将您的 url 发送到服务器之前对其进行编码。

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

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

Edit: Added link to source.

编辑:添加了源链接。