0

I want to pass the value of TextBox1 from one page to another using PostBackUrl. So here is the code for the first page.

<form id="form1" runat="server">
    <div>
        <h2>Working With the Previous Page Object</h2>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Default7.aspx"/>

    </div>
    </form>

Now, here is the code for the page that retrieves the value from the First Page:

protected void Page_Load(object sender, EventArgs e)
    {
        Page previousPage = Page.PreviousPage;
        if(previousPage != null)
        {
            Label1.Text = ((TextBox)previousPage.FindControl("TextBox1")).Text;
        }
    }

Off course I have inserted a Label called "Label1" on the page that retrieves the value of TextBox1 from the First Page.

I have seen lots of Tutorials doing exactly the same thing, but it just does not work for me, I do not know why. Any help is welcomed.

pancy1
  • 491
  • 7
  • 16
  • Is this a webform or webcontent form? – Suprabhat Biswal Dec 08 '15 at 11:03
  • Are you receiving any error/exception here ? – Dhrumil Dec 08 '15 at 11:11
  • 1
    **does not work for me** will not help anyone understand the actual issue. You are getting any error? Have you debugged your code? That's the best way to help yourself. – Rahul Singh Dec 08 '15 at 11:11
  • This is a Web Form, I do not receive any error either on the Screen as a message or on the Console, if I place a Breakpoint in the if Statement, I see that it does not pass the if statement. If I delete the if statement, then I get a nullException error. – pancy1 Dec 08 '15 at 11:20
  • @pancy1 - Is your first page inside Master page? – Rahul Singh Dec 08 '15 at 11:30
  • No, how can I fix it? – pancy1 Dec 08 '15 at 11:39
  • @pancy1 - See the problem is definitely `previousPage` is null so you are not able to find the control. Are you sure you are landing in this page after clicking `Button1` in page 1? Also, do you have any routing configured? – Rahul Singh Dec 08 '15 at 11:47
  • Yes the previousPage statement is not executed. Here is the scenario, the first page is called "Default6.aspx" and the Next Page is called "Default7.aspx", yes when I press the submit button from the First Page it takes me to the Next Page, but without the desired outcome, hope this helps, i have no routing configured – pancy1 Dec 08 '15 at 12:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97282/discussion-between-pancy1-and-rahul-singh). – pancy1 Dec 08 '15 at 12:19

3 Answers3

0

Probably here, the FindControl() is not recognizing any control named TextBox1 which is the ID that you gave in the design code.

You can try using the full Unique ID of that control in the FindControl() like this :

Label1.Text = ((TextBox)previousPage.FindControl("ctl00$ContentPlaceHolder1$TextBox1")).Text;

This Unique ID would be the ID generated at runtime when control is rendered into HTML. You can read it from the HTML source using inspect element in the browser.

As an other option, you can also try finding the ContentPlaceHolder first and then finding the TextBox with the given ID inside it.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • Hi Matt, thank you for your contribution but it did not work. – pancy1 Dec 08 '15 at 11:37
  • @pancy1 - I tried using your code and I succesfully got the result ! I hope you haven't just pasted my answer directly without changing the Control ID. – Dhrumil Dec 08 '15 at 11:38
  • I get TextBox1 in firebug for the Unique ID – pancy1 Dec 08 '15 at 11:43
  • This is quite weird. As you mentioned, you are not using a master page, which means that your page wont have any ContentPlaceHolder. I happened to test same kind of scenario in Firebug and I still got the UniqueID as I have mentioned in my answer. I suggest you once check into chrome as to what the UniqueID is coming out. – Dhrumil Dec 08 '15 at 11:48
  • I get the same ID in Chrome, the problem is that it does not execute the if statement! But I do not know how to fix it. Any help is welcomed – pancy1 Dec 08 '15 at 12:18
0

Sample

WebForm1.aspx

<form id="form1" runat="server" action="WebForm2.aspx" method="post"> 
<div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="true" />
</div>
</form>

WebForm2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Request.Form.GetValues("TextBox1")[0]);
    }
0

WebForm1.aspx

<form id="form1" runat="server"> 
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>

WebForm1.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
        {
            Session["TextBox1Value"] = TextBox1.Text;
            Response.Redirect("WebForm2.aspx");
        }

WebForm2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(Session["TextBox1Value"]);
        }
  • Thanks a lot I am aware of the Session method, I just want to see how to PostBackUrl works as my issue title describes! – pancy1 Dec 08 '15 at 12:50