Passing hidden field from one page to another in q

2019-05-21 03:51发布

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?

6条回答
ゆ 、 Hurt°
2楼-- · 2019-05-21 04:01

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.

查看更多
等我变得足够好
3楼-- · 2019-05-21 04:06
<form method="get">
查看更多
劫难
4楼-- · 2019-05-21 04:12

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楼-- · 2019-05-21 04:14

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>
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-05-21 04:18

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.

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-05-21 04:22

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 );
查看更多
登录 后发表回答