简单HTTP Post返回空Request.Form

时间:2015-03-04 22:04:45

标签: asp.net http-post

我正在尝试一个简单的HTTP帖子,以便在asp.net中将数据从一个表单发布到另一个表单。 发件人页面代码

 <form id="form1" runat="server" method="post" action="CILandingPage.aspx">
<asp:TextBox name="txtUname" runat="server" Width="180px"></asp:TextBox>
<asp:TextBox name="txtPassword" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
 <asp:TextBox name="txtTransaction" runat="server" Width="180px"></asp:TextBox>

,接收者页面有代码

        lblUserName.Text = Request.Form["txtUname"].ToString();
        lblPassword.Text = Request.Form["txtPassword"].ToString();
        lblTransactionID.Text = Request.Form["txtPassword"].ToString();

它抛出NullReferenceException,因为Request.Form对象为空。

我错过了什么?

3 个答案:

答案 0 :(得分:2)

设置要将ASP.NET Web窗体页面发布到的页面的PostBackUrl property for the control to the URL

action添加 PostBackUrl移至Button。而是命名使用 ID 属性值

在Default.aspx中

<form id="form1" runat="server" method="post">
<div>       
    <asp:TextBox ID="TextBox1" name="txtUname" runat="server" Width="180px"></asp:TextBox>
    <asp:TextBox ID="TextBox2" name="txtPassword" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
    <asp:TextBox ID="TextBox3" name="txtTransaction" runat="server" Width="180px"></asp:TextBox>
    <asp:Button ID="button" PostBackUrl="~/CILandingPage.aspx" runat="server" />           
</div>
</form>

在CILAndinaPage.aspx.cs

using System;

public partial class CILandingPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Response.Write(Request.Form["TextBox1"].ToString() +Environment.NewLine);
            Response.Write(Request.Form["TextBox2"].ToString() + Environment.NewLine);
            Response.Write(Request.Form["TextBox3"].ToString());            
        }
    }
}

答案 1 :(得分:1)

您可以使用PreviousPage参考如下:

protected void Page_Load(object sender, EventArgs e)  
{  
// first check if we had a cross page postback  
    if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack))  
    {  
        Page previousPage = PreviousPage;
        TextBox UserName= (TextBox)previousPage.FindControl("txtUname");
        TextBox Password= (TextBox)previousPage.FindControl("txtPassword");
        // we can now use the values from TextBoxes and display them in two Label controls..
        lblUserName.Text = UserName.Text;
        blPassword.Text = Password.Text;
    }  
}  

Page_Load中的此代码将引用已发布数据的上一页&amp;帮助您在目标页面上获得相同的信息。

希望这会有所帮助!!

答案 2 :(得分:0)

由于您要发布跨页面,因此集合项(txtPassword)可能不存在。您可以尝试将每个控件的ClientIdMode设置为static,以便HTTP帖子中使用的id与您在目标页面上的.Form集合中查找的内容相匹配。

有关跨页面发布的详细信息,请查看此文章:https://msdn.microsoft.com/en-us/library/ms178139%28v=vs.140%29.aspx

使用浏览器调试工具(F12)查看HTTP Post正文中传输的内容。

相关问题