views:

77

answers:

3

I know this has got to be the simplest-sounding question ever asked about ASP.Net but I'm baffled. I have a form wherein my visitor will enter name, address, etc. Then I am POSTing that form via the PostBackUrl property of my Submit button to another page, where the fields are supposed to be all re-formed into new hidden fields, then POSTed again to Paypal.

My problem is I cannot get at the values entered by the visitor in the original page. Any time I put in "runat='server'", ASP.Net completely changes the ID of the control, making it impossible to figure out how to access. In the POSTed form I tried Request.Form["_txtFirstName"] and that turned up null. Then I tried ((TextBox)PreviousPage.FindControl("_txtFirstName")).Text and that was null, too. I've tried variations on those. I cannot figure out how I'm supposed to get at these controls. Why does this stuff need to be so difficult?

A: 

What is the name of the TextBox control on the first page? Do not use the clientId, use the ID that it is declared as when calling FindControl, so if it is called ID="TextBox1", use the code below to find it.

Your second approach looks ok, except you missed out the Page.PreviousPage. That shouldn't affect the result though. Have you switched tracing on?

This is the standard syntax from the docs, placed in your target page...

if (Page.PreviousPage != null)
{
    TextBox SourceTextBox = 
        (TextBox)Page.PreviousPage.FindControl("TextBox1");
    if (SourceTextBox != null)
    {
        Label1.Text = SourceTextBox.Text;
    }
}
Daniel Dyson
Also, see patmortech's answer about master pages
Daniel Dyson
Mike at KBS
A: 

In ASP.NET if the control is a server-side control, you simply call it by the ID given to it when coding, not the rendered one.

Markup:

<input type="text" id="myId" runat="server" />

Code behind:

string controlValue = myId.Value;
Oded
A: 

Are you using MasterPages? If so, you have to search for the control inside the content placeholder:

ContentPlaceHolder placeholder = (ContentPlaceHolder)Page.PreviousPage.Master.FindControl("ContentPlaceHolder1");

TextBox previousPageTextBox = (TextBox)placeholder.FindControl("TextBox1");
patmortech
This is also a very good point
Daniel Dyson
Wow, that was indeed the problem. What a nightmare. Thanks patmortech!
Mike at KBS