2

I am trying a simply HTTP post to post data from one form to another in asp.net. sender page code

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

and the receiver page has code

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

it throws NullReferenceException because Request.Form object is empty.

what am i missing?

grace
  • 253
  • 1
  • 5
  • 17

3 Answers3

2

Set the PostBackUrl property for the control to the URL of the page to which you want to post the ASP.NET Web Forms page.

Remove action and add PostBackUrl into Button.Instead name use ID property value.

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());            
        }
    }
}
Elshan
  • 7,339
  • 4
  • 71
  • 106
1

You may use PreviousPage reference as below:

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

This code in Page_Load will get referenced to previous page that has posted the data & helps you get the same on the destination page.

Hope this helps!!

Praveen
  • 86
  • 1
  • 9
0

Since you are posting cross-page, it is likely that collection item (txtPassword) doesn't exist. You can try setting the ClientIdMode of each control to static so that the id used in the HTTP post matches what you are looking for in the .Form collection on the target page.

Check out this article for more info on cross-page posting: https://msdn.microsoft.com/en-us/library/ms178139%28v=vs.140%29.aspx

Use your browser debugging tool (F12) to see what is transmitted in the HTTP Post body.

Andre Oporto
  • 1,236
  • 9
  • 8