2

In an .aspx page I'm filling out a number of hidden form fields with JavaScript and then want to post the form to another .aspx page. It submits the form properly but when I look at the Request.Params["org"] in the page that was called in the form's action it's null.

<script src="prototype.js"></script>
<script>
function doIt() {
    $('org').value = org_int;
    $('frmCheckout').submit();
}
</script>
...
<form id="frmCheckout" method="post" action="checkout.aspx">
<input type="hidden" value="" id="org" name="org">
<input type="submit" onclick="doIt()" value="Submit">
</form>

$ is the prototype notation which was brought in before the example. $ in prototype is the same as document.getElementById('frmCheckout').

MAJOR EDIT 2/1/2021 7:52: Looking for the solution I found a couple of new bits of information but it still doesn't work.

I found PostBackUrl which will change the .aspx's action to another .aspx page. This part works. When submitted, it calls Checkout.aspx

<asp:Button ID="Submit" PostBackUrl="Checkout.aspx" runat="server" Text="Submit" />

But the data is not available in Checkout.aspx... there are no values in Request.Form (which there is supposed to be) or Request.Params.
I've also tried:

<asp:TextBox runat="server" name="jsonString" id="jsonString" value="js" />

But I'm getting an invalid __VIEWSTATE I know I'm supposed to change it but nothing has worked yet.

8
  • js can not transfer data from one page to another. session or cookie may help you to this. stackoverflow.com/questions/17502071/… Commented Feb 1, 2021 at 18:55
  • Temporarily make your hidden element a standard text box. Does it display your expected value before you submit? Commented Feb 1, 2021 at 19:18
  • Jasen: I had been checking the value in debugger but made it a text box to make sure and it's showing the data. Murat: I have literally hundreds of pages that fill form fields with JS and post to classic asp pages... this is the first time with .aspx. So, what's different about .aspx? Commented Feb 1, 2021 at 19:29
  • Are your values actually getting posted? Check with F12. Commented Feb 1, 2021 at 21:16
  • 1
    Using the debugger (F12) to verify that the proper values are being put into the form fields... they are. Target .aspx gets the post but no values there. Commented Feb 1, 2021 at 21:20

4 Answers 4

1

Try the following. In the following example the Javascript needs to be after the form code. Request.Form["org"] is used to get the value of "org" from the posted data. Also, if one uses $ in Javascript, it's important not to use it until it's defined. See javascript dollar sign variable not working.

Test.aspx (works)

<!-- directives -->
<% @Page Language="C#" %>

<html>
   <head> 
      <title> Test Form </title> 
   </head>
   
   <body>
      <h3> Test Form </h3>

      <form runat="server" id="frmCheckout" method="post" action="checkout.aspx">
         <input runat="server" type="hidden" id="org" name="org" value=""/>
         
      </form>

      <script>
          function setInitialVals() {
              document.getElementById('org').value = "3";
              document.getElementById('frmCheckout').submit();
          };

          setInitialVals();
      </script>
   </body>
</html>

checkout.aspx

<!-- directives -->
<% @Page Language="C#" %>

<html>
   <head> 
      <title> Checkout</title> 
   </head>
   
   <body>
      <h3> Checkout </h3>
      
       <% Response.Write("org: " + Request.Form["org"]); %>  
      
   </body>
</html>

The following won't work:

Test.aspx (doesn't work)

Note: The following won't work because the Javascript is executed before the input element is rendered.

<!-- directives -->
<% @Page Language="C#" %>

<html>
   <head> 
      <title> Test Form </title> 
   </head>
   
   <body>
      <h3> Test Form </h3>

      <script>
          function setInitialVals() {
              document.getElementById('org').value = "3";
              document.getElementById('frmCheckout').submit();
          };

          setInitialVals();
      </script>

      <form runat="server" id="frmCheckout" method="post" action="checkout.aspx">
         <input runat="server" type="hidden" id="org" name="org" value=""/>
         
      </form>
   </body>
</html>

Resources

Introducing ASP.NET Web Pages - HTML Form Basics

Sign up to request clarification or add additional context in comments.

1 Comment

I edited the example. I was trying to paint a simple picture trying to focus on the post data not being received, but I confused the issue. Prototype ($) is defined for it's used.
1

Please, change the approach. Consider add an asp button into the form and fire click event for this button; the button properties need to be like this topic of Microsoft Docs How to: Post ASP.NET Web Pages to a Different Page:

<script src="prototype.js"></script>
<script>
function doIt() {
    $('#org').value = org_int;
    $('#btnPostBack').click();
}
</script>
...
<form id="frmCheckout" method="post">
<input type="hidden" value="" id="org" name="org">
<input type="button" onclick="doIt()" value="Submit">
<asp:Button ID="btnPostBack" ClientIdMode="Static" PostBackUrl="~/checkout.aspx"  runat="server" Text="Post Form To Another Page" style="display:none;"/>
</form>

Note: I saw in your code that id's selectors not contains hashtag prefixes in your JS code...

2 Comments

I have tried the <asp:Button with no difference... However! I see you haveClientIdMode="Static". Perhaps I have just been looking for the wrong name! And I'm using "prototypejs" (like jQuery but not). Their shortcut for an object reference is just the $ (prototypejs.org)
I use both prototypejs and jQuery. prototype uses $('name') where jQuery uses $('#name'). There is no problem with $('org') = org_int. If I were using JQuery here, that would be $('#org').val(org_int). When I mix them I use $j('#org')
0

Please,

try change form method attribute from post to get, if you want use "org" as Query String:

$('org').value = org_int;
$('frmCheckout').submit();
...
<form id="frmCheckout" method="get" action="checkout.aspx">
<input type="hidden" value="" id="org" name="org">
</form>

3 Comments

Actually, there are a lot more parameters. I just showed this one for the concept. GET does work fine but POST is a little safer and supports virtually unlimited size.
Ok. I beleive the "Request.Params" only works from current aspx to current code behind; to cross pages, you need to use Request["org"] to get data (please, if possible, validate this in debug mode).
I've tried Request, Request.Params, Request.Form, and the data just isn't being posted.
0

Update:

I downloaded prototype.js and added it to the test code below which seems to work. The only difference between the code you posted and the code below should be that I hard-coded the value of "org_int" in function "doIt": let org_int = 3;.

test.aspx

<% @Page Language="C#" %>

<html>
   <head> 
      <title> Test Form </title> 
   </head>
   
   <body>
      <h3> Test Form </h3>

       <script src="prototype.js"></script>
             
       <script>
           function doIt() {

               try {
                        let org_int = 3;

                        $('org').value = org_int;
                        $('frmCheckout').submit();

                        //document.getElementById('org').value = org_int;
                        //document.getElementById('frmCheckout').submit();
               }
               catch (err) {
                   console.log(err);
                   alert(err);       
               }
           }

       </script>

        <form id="frmCheckout" method="post" action="checkout.aspx">
            <input type="hidden" value="" id="org" name="org">
            <input type="submit" onclick="doIt()" value="Submit">
        </form>
       
   </body>
</html>

checkout.aspx

<% @Page Language="C#" %>

<html>
   <head> 
      <title> Checkout</title> 
   </head>
   
   <body>
      <h3> Checkout </h3>
      
       <% Response.Write("Request.Form[\"org\"]: " + Request.Form["org"] + "<BR>"); %>  
       <% Response.Write("Request.Params[\"org\"]: " + Request.Params["org"]); %>  
      
   </body>
   
</html>

3 Comments

As I said in the edited post the $ is "prototype"... like jQuery and it's the way they reference elements same as getElementById. It's defined in "prototype.js" before the regular script tag.
If you haven't already tried the simple test code I posted, you may want to give it a try.
User9938... re-read my last post about where the $ comes from. Your test post has not included prototype.js. Then you can check prototypejs.org if you want more details. The $ is definitely NOT the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.