繁体   English   中英

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

[英]Getting values posted by a form on target page ASP.NET

我是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来获取表单值,但是我正在学习如何将表单发布到另一个页面。

谢谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM