ASP.NET 2.0 - Accessing controls in the previous page

Rate It (2)

Last post 04-29-2008 8:39 AM by ShivaKarthik. 14 replies.

Sort Posts:

  • ASP.NET 2.0 - Accessing controls in the previous page

    01-12-2006, 12:04 PM
    • Loading...
    • ranganh
    • Joined on 02-11-2004, 11:35 PM
    • India
    • Posts 2,422

     

    One of the problems faced by us in ASP.NET 1.x versions is the inability to transfer data between pages easily. Since ASP.NET 1.x pages postback to the same page by default, and you cannot do a post to another page (unless you remove the runat="server" and other messy things), accessing the controls in the previous page becomes very difficult unlike classic ASP where you have a action tag to specify the new page and you can use the Request.Form to access the previous page values.

    There is a more effective way of accessing the Controls in the previous page in ASP.NET 2.0. Its using the PreviousPage property of the Page.

    Say we have a page Default.aspx with a Textbox "Text1" and a Button "Button1".

    We can access the controls in Default.aspx from another page by the following steps:-

    1. Setting the PostBackUrl property of the Button to the New Page, as follows:-

    <asp:Button ID="button1" Runat=server Text="submit" PostBackUrl="~/NewPage.aspx" />

    2. Then, in the NewPage.aspx.cs, you can access the TextBox control on Default.aspx as follows:-

    public void page_load()
    {
    if(!IsPostBack)
    {
    TextBox tb = (TextBox)PreviousPage.FindControl("Text1");
    Response.Write(tb.Text);}
    }

    Note that a new TextBox tb is declared and typecasted using the PreviousPage and FindControl of the ID of the Control in the Previous Page.  However, to retain the value across postbacks within the new page, its better to assign the text to a TextBox or Label in the NewPage.aspx such that its value is not lost across postbacks.  The reason behind is that, the PreviousPage becomes invalid once you postback in the same page.

    We could still achieve this in ASP.NET 1.x versions by using the Server.Transfer but Server.Transfer has other undesirable effects like URL not updated in the browser and limitation within the application etc., 

    regards,
    Harish

    http://geekswithblogs.net/ranganh
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    02-28-2006, 12:17 AM
    Does this method also work for a CheckBoxList that is dynamically generated at runtime?
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    04-05-2006, 11:41 AM
    • Loading...
    • katit
    • Joined on 07-14-2003, 2:37 PM
    • Posts 2

    I was playing with that for a while yesterday..

    I have website based out of IBuySpy portal. There is panels and user controls loaded into those panels.

    After I postback to different page - I can't find any controls in PreviousPage object. I only see original page controls and none of the dynamic ones. I tried recursive printout of all controls and there is NO custom control data..

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    04-17-2006, 10:02 AM
    • Loading...
    • dodiggitydag
    • Joined on 08-10-2005, 5:33 PM
    • Michigan, USA
    • Posts 51
    Why not store information in the session??
    For data, I just store the unique identifier (primary key) of the table that I retrieved the information from.  It seems like the PreviousPage object would really bloat ASP.NET.
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    10-23-2006, 8:36 AM
    • Loading...
    • Amrits4eva
    • Joined on 09-19-2006, 4:39 AM
    • Posts 4

    Right said, sessions can hold small values that are needed in the project/application, rather than writing long code lines.

    sliding nature of session makes it possible.

     

    Cheers.

    Amrit. 

    Amrit
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    12-07-2006, 12:15 AM

     We can use previous page values by using following method too...

    in first page :
    code markup Portion

       Public ReadOnly Property Tb01() As TextBox
           Get
               Return Tb01
           End Get
       End Property
      
       
    HTML Markup Portion

            Enter :
            <asp:Textbox ID="Tb01" Runat="server" />
            <asp:Button ID="Btn_Post" Runat="server" Text="Nextpage.aspx"
             PostBackUrl="nextPage.aspx" />
           
      
    in Second Page :
    Code Markup
      
        Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            PrvPageValue.Text = PreviousPage.Tb01.Text
        End Sub

    HTML Markup

            <asp:Label ID="PrvPageValue" Runat="server"></asp:Label>

     

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    10-09-2007, 9:35 AM
    • Loading...
    • ilovebbs
    • Joined on 10-09-2007, 1:10 PM
    • Posts 5

    it's very helpful to me .I will try it.thanks.

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    10-17-2007, 1:25 AM
    • Loading...
    • KinjanShah
    • Joined on 10-16-2007, 3:43 AM
    • Ahmedabad, Gujarat, India
    • Posts 42

    Hi,

    I am not convinced to use Session at the places where I need the Value of some control of Previous page only once. Because Session Keeps all the things untill it expires. So, it will make burden to the server. So what I suggest is if some values are needed only for once of the previous page then use PreviousPage property as it is proviced nicely.

    Second thing is that IIS Regularly restarts the application for better reliability, so in that case your session values will get lost.

    Third, If you application is in Web Farm or Web Garden then sharing session is a hassel. Because each Web Farm/Garden application might not be using Intelligent Load Balancer which will re-direct same browser's Request to same server each time.

    So, I suggest to go with PreviosPage.

    Thanks and Regards,

    Kinjan Shah.

    MCAD 

    Thanks and Regards,
    Kinjan Shah,
    MCAD
    ----------------------------------------------------
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    10-23-2007, 2:36 AM
    • Loading...
    • pengruijun
    • Joined on 10-23-2007, 6:14 AM
    • Adelaide, South Australia, Australia
    • Posts 1

    why not using the Querystring?

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    11-26-2007, 8:25 AM
    • Loading...
    • ranganh
    • Joined on 02-11-2004, 11:35 PM
    • India
    • Posts 2,422

    Hi,

    Querystring has limitations in terms of the size as well as security flaws in exposing certain data.  The Previous Page is an excellent property that can be used for accessing just the control's value from previous page.

    Hope this helps.

    Cheers

    regards,
    Harish

    http://geekswithblogs.net/ranganh
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    12-04-2007, 6:53 PM
    • Loading...
    • deddy_one
    • Joined on 11-01-2007, 10:03 AM
    • Posts 65

    Hi, I use the context.items and server.transfer in my current project to pass value between asp page.

    which one is better?

    do you have the list of advantage and disadvantage on different ways.

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    03-10-2008, 5:08 AM

    We can use previous page values by using following method too...

    in first page :
    code markup Portion

       Public ReadOnly Property Tb01() As TextBox
           Get
               Return Tb01
           End Get
       End Property
      
       
    HTML Markup Portion

            Enter :
            <asp:Textbox ID="Tb01" Runat="server" />
            <asp:Button ID="Btn_Post" Runat="server" Text="Nextpage.aspx"
             PostBackUrl="nextPage.aspx" />
           
      
    in Second Page :
    Code Markup
      
        Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            PrvPageValue.Text = PreviousPage.Tb01.Text
        End Sub

    HTML Markup

            <asp:Label ID="PrvPageValue" Runat="server"></asp:Label>

    this code gives an error saying that 'Tbl01' is not a member of System.web.UI.Page.

    ???????????

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    04-20-2008, 6:11 AM
    • Loading...
    • spavkov
    • Joined on 11-25-2007, 10:23 PM
    • Serbia, Kula
    • Posts 26

    Yes we can use Session, Cookies or QueryString to pass information from page to page, but it is not considered a good coding practice since you have the built-in mechanisms in ASP.NET 2.0  to transfer any information from one page to the other.

    Here is the detailed article on the Cross Page Postbacks and how to use them to pass information from page to page, and how to access controls and properties from previous page.

    Hope it helps some newbie developers...

    ASP.NET FAQs


    Click "Mark as Answer" on the post that helped you to help future readers.
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    04-20-2008, 7:19 AM

    Agree with spavkov . when we can avoid  Session, Cookies or QueryString to pass information from page to page then why not. I personally avoid  Session,QueryString  for passing information ,if not very necessary.

    regards

    Dev

  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    04-29-2008, 8:39 AM
    • Loading...
    • ShivaKarthik
    • Joined on 04-15-2008, 9:11 AM
    • Hyderabad
    • Posts 155

    y not use cookies.

     

    Regards,
    B.ShivaKarthik
    Software Engineer
    Hyderabad,India
Page 1 of 1 (15 items)