Page view counter

ASP.NET 2.0 - Accessing controls in the previous page

Rate It (3)

Last post 03-05-2009 6:09 AM by Prathmesh Kolaskar. 21 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,428
    • Points 12,063

     

    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
    • Loading...
    • Texaco Bacon
    • Joined on 02-28-2006, 5:10 AM
    • Posts 2
    • Points 10
    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
    • Points 10

    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
    • Points 255
    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
    • Points 17

    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
    • Loading...
    • stalinthesigan
    • Joined on 12-07-2006, 4:24 AM
    • Chennai
    • Posts 2
    • Points 10

     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
    • Points 6

    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 103
    • Points 533

    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
    http://TricksForProgramming.BlogSpot.com
    ----------------------------------------------------
    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
    • Points 2

    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,428
    • Points 12,063

    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
    • Points 79

    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
    • Loading...
    • kuber.manral
    • Joined on 02-27-2008, 5:20 AM
    • Posts 269
    • Points 1,070

    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.

    ???????????

    Thanks & Regards
    Kuber Singh Manral.
    MCTS [ASP.Net 2.0 Web Client Application]

    Please "Mark As Answer", if any Post helps you. It facilitates to find
    exact solution of the problem.
  • 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
    • Points 144

    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
    Fluent DateTime Project

    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
    • Loading...
    • devbrat_ghosh
    • Joined on 05-11-2007, 4:40 AM
    • New Delhi, India
    • Posts 45
    • Points 105

    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

    with regards
    Dev

    I never lose, some people are just better than me at winning.
  • Re: ASP.NET 2.0 - Accessing controls in the previous page

    04-29-2008, 8:39 AM
    • Loading...
    • ShivaKarthik
    • Joined on 04-15-2008, 1:11 PM
    • Hyderabad
    • Posts 268
    • Points 1,637

    y not use cookies.

     

    Regards,
    B.ShivaKarthik
    Developer
    Hyderabad,India

    -------------------------------------------------
    Don't Forget to Mark as Answer for the post that helped you.
    -------------------------------------------------
Page 1 of 2 (22 items) 1 2 Next >