0

I am new to asp.net and learning it to my own. I just want to submit a form on another asp.net page and want to retrieve all the posted values on that page! I have tried the following code (I am just testing and learning asp.net to my own so this code may have some blunders).

My Default.aspx page (submitting the form with values):

 <body>
   <form id="form1" runat="server" action="formtarget.aspx" method="post" onsubmit="return Validate()">
    <div>
        <asp:Label ID="namelab" Text="Your Name" runat="server"></asp:Label>
        <asp:TextBox ID="namebox" runat="server"></asp:TextBox>
    </div>
    <div>
        <asp:Label ID="agelab" Text="Your Age" runat="server"></asp:Label>
        <asp:TextBox ID="agebox" runat="server"></asp:TextBox>
    </div>
    <div>
        <asp:Button ID="submitbutton" Text="Submit" runat="server"/>
    </div>
   </form>
 </body> 

The formtarget.aspx

  <body>
    <form id="form1" runat="server">
     <div>
       You Entered The Following Details!<br />
       Your Name: <asp:Label ID="namelab" runat="server"></asp:Label><br />
       Your Age: <asp:Label ID="agelab" runat="server"></asp:Label>
     </div>
    </form>
  </body>

The formtarget.aspx.cs (here i want to access the posted values by Default.aspx page form)

 public partial class formtarget : System.Web.UI.Page
 {
   protected void Page_Load(object sender, EventArgs e)
   {
    String name = Request.QueryString["namebox"];
    String age = Request.QueryString["agebox"];

    namelab.Text = name;
    agelab.Text = age;
  }
 }

The code is working fine for me but the page formtarget.aspx is not showing up any values.

I know that I can use the Default.aspx.cs to get my form values but I am just learning how can I post my form to another page.

Thanks

1 Answer 1

2

Request.QueryString is used to access parameters passed using GET; to access parameters passed via POST you should use Request.Form:

public partial class formtarget : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String name = Request.Form["namebox"];
        String age = Request.Form["agebox"];

        namelab.Text = name;
        agelab.Text = age;
    }
}
1
  • oki @Saeb Amini thanks for the answer and giving me detailed difference between the both Thanks again
    – Java Nerd
    Aug 30, 2015 at 13:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.