Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
playing round with multiple forms today trying to get the text value of a textbox on one page to show as a label in the next page after a redirect. im not getting any errors but at the same time its not working either, any help on this.?


Webform Main Controls

Button - ButtonMoveName
Textbox - TextBoxNametoMove

Code...

C#
protected void ButtonMoveName_Click(object sender, EventArgs e)
        {
            Response.Redirect("GrabName.aspx");
        }



Webform GrabName Controls

Label - LabelMovedName

Code...

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage != null)
            {
                TextBox movedNameTextbox =(TextBox)PreviousPage.FindControl("TextBoxNametoMove");
                LabelMovedName.Text = movedNameTextbox.Text;
            }
        }
Posted

Add PostBackUrl for the button

Using Post Back URL:
ASP.NET 2.0 has been introduced Cross page posting. By PreviousPage object you can search controls value from which page you are redirected here. To implement Cross page posting you have to define the PostBackUrl property of page1 button to page2. Like:
ASP.NET
<asp:button id="cmdTransfer" runat="server" text="Transfer" postbackurl="page2.aspx" xmlns:asp="#unknown" />


Now write the below code in page2 load event:
C#
TextBox txtName = (TextBox)(PreviousPage.FindControl("txtName"));
string myname =txtName.Text;
FYI
Passing data/parameters/values from one aspx page to another aspx page[^]
 
Share this answer
 
Comments
webpeon80 26-Nov-11 23:56pm    
perfect, thankyou
RaisKazi 27-Nov-11 0:20am    
Correct Answer. 5ed.
[no name] 27-Nov-11 3:55am    
my 5
Likely it is because the text box control is set to protected. Either set the textbox control to public or the better approach is to add a property to the previous page. Something like below. Note I would not name like I did a page PreviousPage. It is for example only.

You can go though following link to get it work

PreviousPage.FindControl

[^]


Or Instead of this you can simply use ViewState or Session to store textbox value also.
Mark as solution if its solved your problem
 
Share this answer
 
v2
Comments
webpeon80 26-Nov-11 23:23pm    
not sure exactly where i need to unprotect it...

one other thing which ive just tried is adding an else clause to the if previous != null as follows

protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox movedNameTextbox = (TextBox)PreviousPage.FindControl("TextBoxNametoMove");
LabelMovedName.Text = movedNameTextbox.Text;
}
else
labelMovedName.Text = "You Failed";
}

and strangely enough, i got the "you Failed" text showing.. could it be that its not noticing a previous page for some reason.?
Its better to save the text in a session object as
Session["prevoiusPageText"]=TextBoxNametoMove.Text;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900