1

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?

Mike K
  • 1,313
  • 2
  • 18
  • 28

3 Answers3

0

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
  • 13,192
  • 6
  • 42
  • 73
  • Also, see patmortech's answer about master pages – Daniel Dyson May 13 '10 at 08:25
  • Well the control ID is _txtFirstName on the server side. However when I'm onto the next page, doing Request.Form("_txtFirstName") produces a null result. A look at the array of controls in it reveals that my control is now ctl00$_contentPlaceHolder1$_txtFirstName, which is unrecognizable, obviously. I thought that by using a POST, having Request.Form() available would solve my problem (prior I had done a Response.Redirect & there were no Request.Form contents). Did not expect to see all the ID's changed. PreviousPage winds up working (see below answer) but what'd we do before 2.0?? – Mike K May 13 '10 at 15:07
0

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
  • 489,969
  • 99
  • 883
  • 1,009
0

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
  • 10,139
  • 5
  • 38
  • 50