Passing parameter from one one ASPX to another ASPX ?

Last post 06-30-2005 10:12 AM by tarjei. 10 replies.

Sort Posts:

  • Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 6:12 AM
    • Member
      586 point Member
    • bips_kr
    • Member since 08-28-2003, 10:28 AM
    • Bangalore India
    • Posts 146
    Hi All I am using C# for an web application and I wants to pass on some of the information/data from one ASPX page to another I am avoiding to use the session object, Please advise some solution to it Thanks
    ----regards

    Bipul Kumar

  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 8:34 AM
    • Participant
      760 point Participant
    • dcyoung
    • Member since 03-31-2003, 1:00 PM
    • Posts 153
    Pass the parameter in the url

    eg page.aspx?parameterstring1&paramterstring2


    on the receiving page (c#)
    strParam1 = Request.QueryString[0];
    strParam2 = Request.QueryString[1];

  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 9:07 AM
    • Contributor
      4,945 point Contributor
    • tarjei
    • Member since 04-27-2005, 5:33 AM
    • Oslo, Norway
    • Posts 988
    Hello,

    You can achieve this by using Server.Transfer().

    Make public properties on the page you want to transfer from, and use Server.Transfer in a button event handler to display the second page. On the second page, you can get to the public properties on the first one using Page.Context.Handler.

    An example.
    WebForm1:
    protected TextBox myTextBox;
    public string MyTextBoxValue {
      get { return myTextBox.Text; }
    }


    WebForm2, inside Page_Load:
    if (!IsPostBack) {
      WebForm1 webForm1 = Context.Handler as WebForm1;

      if (null != webForm1) {

        Response.Write(webForm1.MyTextBoxValue);
      }
    }



    --
    Tarjei

  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 9:10 AM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    if you want to pass "simple" values, you can put them in the query string for the URL of the destination page.
    For example, if you want to pass the parameters "action" and "id" from Page1.aspx to Page2.aspx, you can do (from Page1):

    Response.Redirect("Page2.aspx?action=view&id=1234");

    The querystring starts with a question mark '?' and contains name-value pairs, each one separated by '&'

    In Page2.aspx, you can access the querystring from the property Request.QueryString. You will obtain a NameValueCollection
    that can be accessed also by index. For example:

    string action = Request.QueryString["action"];
    string id = Request.QueryString["id"];

    If you want to pass anything to your destination page (i.e. objects), you can expose them through public properties in Page1.aspx.
    Then, in Page2.aspx, you can retrieve the object representing your Page1 WebForm.
    Let's suppose you want to access an ArrayList. In Page1.aspx codebehind, you'll have a public property:

    public ArrayList Arr
    {
       get { return myArr;}
       set { myArr = value;}
    }

    On Page1.aspx, you will request Page2.aspx with the statement:

    Server.Transfer("Page2.aspx");

    On Page2.aspx you can get the form that requested the page (e.g. Page1):

    Page1 handler = (Page1)Context.Handler;

    Then, you can access the public properties of the Page1 WebForm:

    ArrayList arr = handler.Arr;
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 9:25 AM
    • Member
      250 point Member
    • timoth
    • Member since 06-13-2005, 2:00 PM
    • Posts 52

    The way I have done this so you don't need to use session is:

    1. Make a serializable class like this:

    <Serializable()>
    Public Class RefundVars
             public myVar as string
             public myVar2 as string
    End Class

    It has to be serializable because you may want to add it to viewstate if your page postbacks to itself so you can remember it

    2. Then fill this class before you redirect to the next page and add it to CONTEXT.Items like this:

    context.Items.Add("refundInfo", refundInfo)

    3. Then user server.transfer to go to the next page:

    Server.Transfer(strWebPageName)

    4. Then in the next page you can pick up the variables:

    refundInfo =
    CType(context.Items("refundInfo"), RefundVars)

    --------------------
    If this is a fundamental thing of the whole application, you can make an ancestor code behind page class and code the page_load event as follows:

    If
    (Page.IsPostBack = False) Then
             'first time loading the page
             If Not IsNothing(context.Items("refundInfo")) Then
                         'catch passed variables and store in page level variable
                         'for page processing or passing to next page
                         refundInfo = CType(context.Items("refundInfo"), RefundVars)
                         'save variable in viewstate for postbacks of the web page
                         viewState("refundInfo") = refundInfo
             End If
    Else
              'post back of the page, unload our variable from viewstate
              If Not IsNothing(viewstate("refundInfo")) Then
                         refundInfo = CType(viewstate("refundInfo"), RefundVars)
              End If
    End If

    And when you transfer to another page, use this ancestor function:

    Public
    Sub passInfoToNextPage(ByVal strWebPageName As String)

    If
    Not IsNothing(refundInfo) Then
             'add to context item
             If Not IsNothing(context.Items("refundInfo")) Then
                      context.Items.Remove("refundInfo")
             End If
             context.Items.Add("refundInfo", refundInfo)
    End If
    Server.Transfer(strWebPageName)


     

  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 9:30 AM
    • Star
      7,894 point Star
    • fadil1977
    • Member since 08-23-2004, 4:43 AM
    • london
    • Posts 1,581
    this is lovley solution... but what about if  you have more than one value and more than control type.... can you use the same if yes please provide code !!
    Fadil Alnassar
    www.fadilalnassar.com | FREE Nodil Tab Control
    http://www.mefranchising.com
  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 9:50 AM
    • Star
      13,980 point Star
    • stevenbey
    • Member since 10-28-2002, 8:56 AM
    • United Kingdom
    • Posts 2,807
     fadil1977 wrote:
    this is lovley solution... but what about if  you have more than one value and more than control type.... can you use the same if yes please provide code !!

    You don't have to create a Property for each Control, as long as each Control is defined as public. For example:

    WebForm1:
    public TextBox myTextBox;

    WebForm2, inside Page_Load:
    if (!IsPostBack) {
      WebForm1 webForm1 = Context.Handler as WebForm1;

      if (null != webForm1) {

        Response.Write(webForm1.myTextBox.Text);
      }
    }


    Steven Bey

    Recursion: see Recursion
  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 10:12 AM
    • Contributor
      4,945 point Contributor
    • tarjei
    • Member since 04-27-2005, 5:33 AM
    • Oslo, Norway
    • Posts 988
     fadil1977 wrote:
    this is lovley solution... but what about if  you have more than one value and more than control type.... can you use the same if yes please provide code !!


    You can of course add more public properties, or do it Steven's way by exposing the whole textbox as a public variable (less typing too..). Anything you make public is accessible. I like the string property way myself, since there propably isn't anything else than the .Text property you want from the textbox.

    --
    Tarjei
  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 10:17 AM
    • Star
      13,980 point Star
    • stevenbey
    • Member since 10-28-2002, 8:56 AM
    • United Kingdom
    • Posts 2,807
     tarjei wrote:
    I like the string property way myself, since there propably isn't anything else than the .Text property you want from the textbox.

    --
    Tarjei

    You obviously like typing a lot more than I do Wink [;)]

    Steven Bey

    Recursion: see Recursion
  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 11:01 AM
    • Star
      7,894 point Star
    • fadil1977
    • Member since 08-23-2004, 4:43 AM
    • london
    • Posts 1,581
    Hi tarjei!!!!

    do you mean that you have another way to do it??? can you please include a code sample ?? if you do not mind....
    Fadil Alnassar
    www.fadilalnassar.com | FREE Nodil Tab Control
    http://www.mefranchising.com
  • Re: Passing parameter from one one ASPX to another ASPX ?

    06-30-2005, 11:18 AM
    • Member
      175 point Member
    • mw:17:61
    • Member since 06-20-2005, 8:17 PM
    • Antigua and Barbuda
    • Posts 35

    You may want to distinguish parameters visible to all users, visible to current session or to current request. There is an approach that halps to keep everthing together.
    public abstract class MyContext
    {
       private const string SessionParamKey = "xxxx";
       private const string GlobalParamKey= "xxxx";
       private const string CurrentRequestParamKey= "xxxx";

       public static MyTypeOne GlobalParam
       {
          get
          {  
             return System.Web.HttpContext.Current.Cache[GlobalParamKey];
          }
          set
          {
             System.Web.HttpContext.Current.Cache[GlobalParamKey] = value;
          }
       }
       public static MyTypeTwo SessionParam
       {
          get
          {  
             return System.Web.HttpContext.Current.Session[SessionParamKey];
          }
          set
          {
             System.Web.HttpContext.Current.Session[SessionParamKey] = value;
          }
       }
       public static MyTypeTwo CurrentRequestParam
       {
          get
          {  
             return System.Web.HttpContext.Current.Items[CurrentRequestParamKey];
          }
          set
          {
             System.Web.HttpContext.Current.Items[CurrentRequestParamKey] = value;
          }
       }
    }

    +&up;
Page 1 of 1 (11 items)