pancy1
pancy1

Reputation: 511

Using ASP.Net PostBackUrl to pass value to a Label

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.

Upvotes: 0

Views: 1142

Answers (3)

Tonton Sevilla
Tonton Sevilla

Reputation: 11

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"]);
        }

Upvotes: 0

Tonton Sevilla
Tonton Sevilla

Reputation: 11

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

Upvotes: 0

Dhrumil
Dhrumil

Reputation: 3204

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.

Upvotes: 0

Related Questions