获取表单在目标页面ASP.NET上发布的值

时间:2015-08-30 13:40:48

标签: c# asp.net forms

我是asp.net的新手并且是我自己学习的。我只想在另一个asp.net页面上提交一个表单,并希望检索该页面上的所有发布值!我尝试了以下代码(我只是测试和学习asp.net到我自己,所以这段代码可能有一些错误)。

我的Default.aspx页面(提交带有值的表单):

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

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>

formtarget.aspx.cs(这里我想通过Default.aspx页面形式访问发布的值)

 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;
  }
 }

代码对我来说很好,但页面formtarget.aspx没有显示任何值。

我知道我可以使用Default.aspx.cs来获取我的表单值,但我只是在学习如何将表单发布到另一页。

由于

1 个答案:

答案 0 :(得分:2)

Request.QueryString用于访问使用GET传递的参数;要访问通过POST传递的参数,您应该使用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;
    }
}