Pages

Tuesday, September 20, 2011

Methods to pass values between ASP.Net web pages

Whether or not the source and target pages in same applciation, the data can be passed by following methods
               1. Using query string
               2. Using Get HTTP Post information from the source page

Query string:
     - Can add values to the qusery string at the end of URL.
     - Never pass sensitive data using query string, as its always visible in the URL.
     - To retrieve the value S= Request.QueryString["QueryStringName"];    

Get HTTP Post information from the Source Page:
     - When the source page uses the HTTP Post action to navigate, can retrieve posted values from the FormCollection in the target page.
     - Can get only the post values, cannot read values of arbitarary controls on the page.

When the Source and Target are in same application, data can be passed by the following methods:
                1. Using session state
                2. Using Public property values from source page
                3. Getting control information from the source page in the same application.

Session state:
     - Informations in the session state are available to all pages in the current application, for the specified time.
     - Takes server memory.
     - Stored until the session is expired, which can be more overheaf than you want for simple passing information to teh next page.
     - To store and retrieve
               Session["SessionName"] = "Value";
               Value = Session["SessionName"].ToString();

Getting Public property values from Source page:
    - Add public properties in the source page that expose information you want to share between pages.
    - Read values of the properties in the target page.
    - It works in 2 situations:
            - When the source page cross posts to the target page.
            - When you call the Transfer method to transfer exception from the source to the target page on the server.

Getting control information from the Source page:
    - Can use this strategy if the source page does not expose public properties containing the information you want.
    - Ex. TextBox txt = (TextBox)PreviousPage.FindControl("id");

No comments:

Post a Comment