我正在尝试一个简单的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对象是空的。
我遗漏了什么?
发布于 2015-03-04 17:26:18
设置要将Web页张贴到的页面的PostBackUrl
属性为指向URL的控件。。
删除action
和,将 PostBackUrl
添加到Button
.Instead name 中,使用 ID属性值。
In 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>
In 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());
}
}
}
发布于 2015-03-05 00:55:17
您可以使用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中的这段代码将被引用到发布数据的上一页&帮助您在目标页面上获得相同的信息。
希望这能帮上忙!
发布于 2015-03-04 17:14:22
由于您是跨页投递,很可能不存在集合项(txtPassword)。您可以尝试将每个控件的ClientIdMode设置为静态,以便HTTP中使用的id与您在目标页面上的.Form集合中查找的id匹配。
查看这篇文章,了解更多关于跨页张贴的信息:https://msdn.microsoft.com/en-us/library/ms178139%28v=vs.140%29.aspx
使用浏览器调试工具(F12)查看HTTP正文中传输的内容。
https://stackoverflow.com/questions/28865939
复制