Passing hidden field from one page to another in querystring

大憨熊 提交于 2019-12-22 15:01:10

问题


I want to pass a query in a hidden filed from 1 page to another by querystring. Can anyone help me out with the logic?


回答1:


It's worth taking the time to learn jQuery. It's not very complicated, and it makes writing javascript much easier. There are also many jQuery plugins, such as jquery.url.

Also, as other posters have suggested, you may not wish to put the hidden field's value in the query string if you care about it being displayed to the user. However, if the data is present in a hidden field it will always be possible for a user to find it if they care to look.

If you really do want to put the hidden field in the query string and then extract it via non-jQuery javascript:

hiddenFieldPage.aspx

This form will take the user to processingPage.aspx?datum=someValue when it is submitted. You could probably also just use an ordinary link if nothing else needs to be submitted at the same time.

<form method="GET" action="processingPage.aspx">
    <input type="hidden" name="datum" value="someValue">
    <input type="submit">
</form>

or, inserting the value from code-behind:

RegisterHiddenField("datum", "someValue");

processingPage.aspx

This script will pop-up an alert box with the value of "datum" from the URL - assuming the form's method is set to "GET":

    <script type="text/javascript">

        function getUrlParam( key ) {

            // Get the query and split it into its constituent params
            var query = window.location.search.substring(1);
            var params = query.split('&');

            // Loop through the params till we find the one we want
            for( var i in params ) { 
                var keyValue = params[i].split('=');
                if( key == keyValue[0] ) {
                    return keyValue[1];
                }
            }

            // Didn't find it, so return null
            return null;
        }

        alert( getUrlParam("datum") );
    </script>

If the form's method was set to "POST" (as it usually would be in ASP.NET), then "datum" won't be in the query string and you'll have to place it on the page again:

RegisterHiddenField( "datum", Request.Form["datum"] );

To retrieve the hidden value on the second page:

var datum = document.Form1.item("datum").value;
alert( datum );



回答2:


You can easily submit a form on one page that points to another page using the action parameter. For instance, inside of page1.aspx put the following:

<form action="page2.aspx" method="GET">
  <input type="hidden" name="username" value="joenobody" />
  <input type="submit" />
</form>

Since you're using "GET" as the method instead of "POST", you could potentially use Javascript to parse the URL and get the value that was passed. Alternatively, you could use ASPX to store the value of the "username" field somewhere else on the page. I don't know ASPX (or ASP, or anything Microsoft really), but if you can find a way to output something like the following (and are using jQuery), it may do what you require. Honestly though, it sounds like you are going about something all wrong. Can you modify your question to be a bit more specific about what the general object is that you are attempting to accomplish?

<div id="some_div"><%= Request.form("username") %></div>

<script type='text/javascript'>
  var value_needed = $('#some_div').html();
</script>



回答3:


<form method="get">



回答4:


Assuming you mean hidden in the HTML form sense, your field will be submitted along with all the other fields when the form is submitted. If you are submitting via GET, then your "hidden" field will show up in plain text in the URL. If you don't want the data in the hidden field to be accessible to users, don't put an understandable value in that field.




回答5:


If you are using aspx, you do not need to parse the query string using JavaScript, or even use <form method="GET" ...>. You can POST the form to the second aspx page, extract the value in C# or VB then write it to a client-side JavaScript variable. Something like this:

page1.aspx:

<form method="POST" action="page2.aspx">
    <input type="hidden" name="myHiddenServerField" value="myHiddenServerValue">
    <input type="submit">
</form>

page2.aspx:

<script type="text/javascript">
var myHiddenClientValue = '<%= Request.Form['myHiddenServerField']; %>';
</script>

The above would set the client-side JavaScript variable called myHiddenClientValue to a value of 'myHiddenServerValue' after the POST.

This can be a bad idea because if myHiddenServerField contains single quotes or a newline character, then setting it on the client in page2.aspx can fail. Embedding ASP.NET Server Variables in Client JavaScript and Embedding ASP.NET Server Variables in Client JavaScript, Part 2 deals with specifically these issues, and solves them with a server-side class that ensures values being written to the client are escaped correctly.




回答6:


If you use method="get" on an HTML form then any hidden inputs in that form will be converted to query parameters.

See also Jeremy Stein's answer.



来源:https://stackoverflow.com/questions/1455575/passing-hidden-field-from-one-page-to-another-in-querystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!