在javascript上添加查询字符串值

时间:2012-01-30 10:02:49

标签: c# javascript asp.net window.open

如何将我的查询字符串的值传递给javacript?这意味着获取关于curent windows值的查询字符串并将其传递给javascript以打开新页面。

例如:/FicheClient.aspx?Item=Tarif&Id=850001我想将Id = 850001传递给window.open('Tarif_Report.aspx?Id=????')

 <dx:ASPxButton ID="ASPxButton_RptTarif" runat="server" Text="Voir" AutoPostBack="False">
                          <ClientSideEvents
                            Click="function (s, e) { e.processOnServer = false; window.open('Tarif_Report.aspx?Id=????'); }" />
                        </dx:ASPxButton>

提前谢谢你。 甜菊

5 个答案:

答案 0 :(得分:1)

 /*
* <summary>
* Get the querystring value
* </summary>
* <param name="key">A string contains the querystring key</param>
* <param name="defaultVal">Object which get returns when there is not key</param>
*
*/

function getQuerystring(key, defaultVal) {
    if (defaultVal == null) {
        defaultVal = "";
    }
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null) {
        return defaultVal;
    }
    else {
        return qs[1];
    }
}

试试这个。

答案 1 :(得分:0)

将QueryString的值存储在Label或HiddenField中,并从document.getElementById('Label')。value中获取存储的值。将此值传递给window.open url。

答案 2 :(得分:0)

您可以使用window.location.search

获取网址

检查How can I get query string values in JavaScript?是否有一个很好的答案,可以将Querystring解析成有用的东西。

因此,您可以通过

访问您的参数
window.open('/Tarif_Report.aspx?Id=' + urlParams["Id"]);

答案 3 :(得分:0)

  1. 在页面中创建公共属性。
  2. 在页面加载事件期间,通过从查询字符串中获取值来设置该属性。
  3. 在ASPX页面中使用&lt;%= this.PropertyName获取值; %GT;

答案 4 :(得分:0)

我明白了:

 <script type="text/javascript">


        function getParameterByName(name)
        {
          name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
          var regexS = "[\\?&]" + name + "=([^&#]*)";
          var regex = new RegExp(regexS);
          var results = regex.exec(window.location.href);
          if(results == null)
            return "";
          else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
        }
      </script>


     <dx:ASPxButton ID="ASPxButton_RptTarif" runat="server" Text="Voir" AutoPostBack="False">
                                                                    <ClientSideEvents
                                                                    Click="function (s, e) { e.processOnServer = false; window.open('../Tarif_Report.aspx?ClientID=' + getParameterByName('Id')); }" />
                                                                </dx:ASPxButton>

谢谢大家