C# Posting form from JavaScript to another .aspx but no parameters in Params

Viewed 571

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.

4 Answers

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

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...

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>

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>
Related