views:

4964

answers:

6

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?

A: 
<form method="get">
Jeremy Stein
Thanks Jeremy. So you suggest that it requires me to post a form and put hidden fields in the page. It will be really helpful if you can be a bit more ellaborative.
archana roy
More ellaborative: http://www.w3.org/TR/html401/interact/forms.html
Jeremy Stein
A: 

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.

ctford
Thanks for the response.Can you be a bit more descriptive?I want to pass the value ina hidden variable from a aspx.cs page and want to fetch it in the javascript fucntion of another aspx page.
archana roy
A: 

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.

dnagirl
+1  A: 

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>
Topher Fangio
Thanks a ton Topher for the reply. But i dont have knowledge about jquery.Can you tell me how to fetch the value in a normal javascript?
archana roy
Thanks Topher for the reply. But can you tell me what should I write if my page is page2.aspx.cs i.e. the code behind page? We cant write the form tag right in the code behind page.I dont have permission to modify the aspx page. Please give your suggestions
archana roy
Sorry archana, I don't really know enough to help you, but it looks like Grant Wagner answered you below :-)
Topher Fangio
A: 

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.

Grant Wagner
Thanks Garner for the reply.But can you tell me what should I write if my page is page1.aspx.cs i.e. the code behind page? We cant write the form tag right in the code behind page.I dont have permission to modify the aspx page.Please give your suggestions.
archana roy
@archana: View > Source of *page1.aspx*, find the `name="the_name"` of the field you want to capture, then you can use `<script>var clientSideJSVariable = '<%= Request.Form['the_name'] %>';</script>` on *page2.aspx* to assign the value to a client-side JavaScript variable.
Grant Wagner
+1  A: 

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 );
ctford
Thanks a ton for your reply.But can you tell me what should I write if my page is hiddenFieldPage.aspx.cs i.e. the code behind page? We cant write the form tag right in the code behind page.I dont have permission to modify the aspx page.Please give your suggestions.
archana roy
Are you sure you need to put the value in the querystring? Perhaps the Session object is what you are after:Session["key"] = "value";If you put a value in the session in the code-behind you can retrieve it on other pages.
ctford