-1
// SourcePage.aspx 
public partial class SourcePage: System.Web.UI.Page 
{ 
    protected void btnSearch_Click(object sender, EventArgs e) 
    { 
        //...... 
        //...... 

            while (rdr.Read()) 
            { 
                txtName.Text = rdr["Name"].ToString(); 
            } 
            rdr.Close(); 
    } 

    public string GetName 
    { 
        get { return txtName.Text; } 
    } 

    protected void btnServer.Transfer_Click1(object sender, EventArgs e) 
    { 
       Server.Transfer("TargetPage1.aspx"); 
       Server.Transfer("TargetPage2.aspx"); 
    } 
} 

//TargetPage1.aspx   
protected void Page_Load(object sender, EventArgs e) 
{ 
            SourcePage SP; 
            SP = (SourcePage)Context.Handler; // I get runtime error here: unable to cast object of type 'ASP.TargetPage_aspx' to type 'SourcePage' 
            txtTPName.Text = p.GetName; 
} 

Where I am wrong? Can't really figure it out. I need to transfer the values from the sourcepage to other target pages, and I don't want to use several queries. Any kind of help will be highly appreciated !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Try with Page.PreviousPage

Using this property you'd be able to access all original page controls.

TextBox txtName = (TextBox)Page.PreviousPage.FindControl("txtName");
txtTPName.Text = txtName.Text;
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • Could you please provide an example for Page.PreviousPage? I mean exact (what is where?); because I have tried with this and had problems too – Earths Basement Jun 20 '12 at 15:59
  • @M Rokon Jaman: Check the msdn reference: http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage.aspx – Claudio Redi Jun 20 '12 at 16:40
  • @M Rokon Jaman: session is a valid mechanism to share information across pages so if worked well for you then this is resolved :) – Claudio Redi Jun 21 '12 at 12:27
  • @MRokonJaman: I rolled back your edit on this answer. It changed it completely and should have been a new answer (which you already posted anyway) – ThiefMaster Jun 22 '12 at 13:43
0

Hey guys I solved the issue using session variables instead, here is how..

Source page : codebehind: after the connection follow..

SqlDataAdapter sda = new SqlDataAdapter();          
sda.SelectCommand = cmd;         
sda.Fill(dt);          
foreach (DataRow Row in dt.Rows)         
{             
txtSource.Text = Row["Name"].ToString();      
    }  
        Session.Add("sp", dt);  

Then in the Target page :

    DataTable dt = Session["sp"] as DataTable;    
 foreach (DataRow Row in dt.Rows)    
 {      
   txtTarget.Text = Row["Name"].ToString();  
   } 

Now I can initialize this datatable in any other pages and can have required data from there.