Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

how do I access an instantiated object from a 2nd page C#

In Default.asp.cs, I have:
public partial class _Default : System.Web.UI.Page
{
    MyCriteria mc =new MyCriteria();

    protected void Page_Load(object sender, EventArgs e)
    {

        mc.PressedMaster="abc";
        mc.YYYY="2013";
   }
}

Open in new window

Now, I want to access it from Page1.asp.cs:
public partial class _Page1 : System.Web.UI.Page
{
    //MyCriteria mc =new MyCriteria();

    protected void Page_Load(object sender, EventArgs e)
    {

       // mc.PressedMaster="abc";
       // mc.YYYY="2013";
   }
}

Open in new window

Question: How can I read "2013" value stored in YYYY property before in Default.asp.cs?

Is it possible without use of session variable?

Thank you.
0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
  • 2
1 Solution
 
MrunalCommented:
One way is put that object in session if these varies for user to user and access that same session in another page.

Another way is create public property in page1 and assign value to that property from default page.

Hope this helps you.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re:> Another way is create public property in page1 and assign value to that property from default page.

This is my question. How to do this.
0
 
MrunalCommented:
okay.

In page1, add below code:

public MyCriteria MyCriteriaObject { get; set; }

Open in new window


From Default page, access this property and set the value.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Mrunal,

re:> From Default page, access this property and set the value.

As you can see above, I am accessing the properties of the object in Default page. Therefore, accessing it from Default page is not the issue. The question is how to access the same object created in the Default.aspx.cs earlier from Page1.aspx.cs .

I appreciate if you plug your solution in the code below:
public partial class _Page1 : System.Web.UI.Page
{
    //MyCriteria mc =new MyCriteria();

    protected void Page_Load(object sender, EventArgs e)
    {

       // mc.PressedMaster="abc";
       // mc.YYYY="2013";
   }
}

Open in new window


Thanks,

Mike
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now