Reputation: 207
I am trying to retrieve text box values from my main asp page. I ask the user to enter his name and surname into their respective text boxes. Then I click on a submit button and the button takes me to another web page called DefaultPage.aspx... The button does take me to the new page, but the text boxes values are not sent or displayed on the new page.
Here is my Code:
ASP Code for the button:
asp:Button ID="Submit" runat="server" Text="Button" OnClick="Submit_Click"
Here is my Main Page's code:
protected void Submit_Click(object sender, EventArgs e)
{
Response.Redirect("DefaultDetails.aspx?FirstName=" + txtFirstName.Text + "&LastName=" + txtLastName.Text);
}
And here is my Default/new pages code:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
string firstName = Request.QueryString["FirstName"];
string lastName = Request.QueryString["LastName"];
}
}
Here is my code for the 2 text boxes:
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
When i insert a break at the first string line (ie firstName on the new page) the values inside of firstName and LastName are equal to null.
Upvotes: 2
Views: 2645
Reputation: 32561
You should use:
Server.Transfer("DefaultDetails.aspx?FirstName=" + txtFirstName.Text + "&LastName=" + txtLastName.Text);
From the Page.PreviousPage Property article:
When you use the Transfer method or use cross-page posting to transfer processing from one ASP.NET page to another, the originating page contains request information that might be required for the destination page. You can use the PreviousPage property to access that information.
If the current page is being rendered as a result of a direct request (not a transfer or cross-post from another page), the PreviousPage property contains null.
Actually, if you would transfer the request, the following should be enough:
Server.Transfer("DefaultDetails.aspx");
In the other page, you could get the values like this:
if (PreviousPage != null)
{
string firstName, lastName;
var txtFirstName = (TextBox)PreviousPage.FindControl("txtFirstName");
var txtLastName = (TextBox)PreviousPage.FindControl("txtLastName");
if (txtFirstName != null)
firstName = txtFirstName.Text;
if (txtLastName != null)
lastName = txtLastName.Text;
}
Upvotes: 0
Reputation: 490
The reason is PreviousPage is null. So your firstName and lastName will be not initialized.
"When you use the Transfer method or use cross-page posting to transfer processing from one ASP.NET page to another, the originating page contains request information that might be required for the destination page. You can use the PreviousPage property to access that information. If the current page is being rendered as a result of a direct request (not a transfer or cross-post from another page), the PreviousPage property contains null."
Please reference enter link description here
Upvotes: 0
Reputation: 934
You can try this
string Ln=Request["txtFirstName"].toString();
string Fn=Request["txtLastName"].toString();
Upvotes: 0
Reputation: 14935
One the new page, remove previous_page condition and check weather you see values or not. Where is this previousPage variable being set
protected void Page_Load(object sender, EventArgs e)
{
string firstName = Request.QueryString["FirstName"];
string lastName = Request.QueryString["LastName"];
}
Upvotes: 1