简体   繁体   English

使用javascript,将查询字符串从aspx传递到html,再传递回aspx,链接问题

[英]pass query string from aspx to html back to aspx, using javascript, link problem

I am passing a querystring from my previous page to this page and then i want to pass it to the next page, but it isn't working. 我正在将查询字符串从上一页传递到此页面,然后将其传递到下一页,但是它不起作用。

<script type="text/javascript">
           function qs(search_for) {
        var query = window.location.search.substring(1);
        var parms = query.split('&');
        for (var i=0; i<parms.length; i++) {
            var pos = parms[i].indexOf('=');
            if (pos > 0  && search_for == parms[i].substring(0,pos)) {
                return parms[i].substring(pos+1);;
            }
        }
        return "";
    }
    </script>

here is the link 链接在这里

<a href="http://www.TEST.com/TEST/TEST/TEST.aspx?comp=" & <script type="text/javascript"> document.write(qs("comp")); </script> & "name=test" >CLICK HERE </a></font></b></p>

只需将其粘贴到隐藏字段中,然后使用简单的document.getElementById()进行检索,可能会更容易,更灵活且更易于维护。

  • Page 1 Creates URL querystring 第1页创建URL查询字符串

  • Page 2 add this to your second page 第2页将此添加到第二页
    <asp:hiddenfield runat="server" id="hdncomp" value=""/> <asp:hiddenfield runat="server" id="hdnname" value=""/> <asp:hiddenfield runat="server" id="hdncomp" value=""/> <asp:hiddenfield runat="server" id="hdnname" value=""/>

    Now, in your Page 2 Load event run the following code. 现在,在您的Page 2 Load事件中,运行以下代码。

     Me.hdncomp = Request.QueryString("comp") Me.hdnname = Request.QueryString("name") 
  • Finally, when you go to Page 3 Use the values from hdncomp and hdnname as your parameter values. 最后,当您转到第3页时。请使用hdncomp和hdnname中的值作为参数值。 For example: 例如:

Response.Redirect("page3.aspx?comp=" & hdncomp & "&name=" & hdnname)

Untested code 未经测试的代码

well, your syntax is very wrong. 好吧,您的语法非常错误。 you cannot write a new element before closing the first elements tag : <element <element2 /> /> . 您不能在关闭第一个elements标签之前编写新元素: <element <element2 /> /> here's a function that you can use : 这是可以使用的功能:


function createLink(address,param,text){
   address += ((address.indexOf('?') == -1) ? "?" : "&") 
      + param 
      + "="
      + qs(param);
    document.write(''+text+'');
}
and then, when you want to insert the desired link by inserting a script tag calling the "createLink" function with the desired parameters : 然后,当您想通过插入带有所需参数的调用“ createLink”函数的脚本标签来插入所需链接时:
 <script type="text/javascript"> createLink("http://www.TEST.com/TEST/TEST/TEST.aspx?name=test","comp","CLICK HERE"); <script/> 

Hope this helps! 希望这可以帮助!

ps : you should probably use only one semicolon and not two at a time. ps:您可能应该只使用一个分号,而不要一次使用两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM