简体   繁体   中英

Using values from one aspx.cs file to another aspx.cs file with a common class

common class file common.cs : This file, I have added by clicking add->new items-> class

public class common
{
  public int v,n;
   public int da()
     {
         return n= v;
     }

}

Another file: It's an webpage file name is a1.aspx.cs:

common c = new common();

c.v = Convert.ToInt32(TextBox1.Text);
c.da();
Response.Redirect("ulogin.aspx");

the value from a text box stores in cv variable

So, now, I want the value which was given in the textbox1.text in another webpage file named as ulogin.aspx.cs

I used this code:

common d=new common();
d.da();

Label1.Text = Convert.ToString(d.n);

but after running it shows the value as 0.....

In a web application, you'll need to persist the information somewhere common (typically Session for per user info or Application for per application info) so that it can be used between different pages & user controls in your application.

I'd suggest adding a Session backed property to your page & usercontrol which accesses a common Session["variable"]. Something like the following.

(ie lets imagine your code was being exectued on a button click)

a1.aspx.cs

public int ValueToStore 
{
   get
   { 
       return Session["ValueToStore"] != null
           ? (int)Session["ValueToStore"]
           : 0
   }
   set
   {
       Session["ValueToStore"] = value;
   }
}

protected void Button1_Click(object sender, EventArgs e)
{ 
    ValueToStore = Convert.ToInt32(TextBox1.Text);
    Response.Redirect("ulogin.aspx");
}

ulogin.aspx.cs

public int ValueToStore 
{
   get
   { 
       return Session["ValueToStore"] != null
           ? (int)Session["ValueToStore"]
           : 0
   }
   set
   {
       Session["ValueToStore"] = value;
   }
}

protected void Page_Load(object sender, EventArgs e)
{
     Label1.Text = ValueToStore.ToString();
}    

As you can see, you now have some code duplication between the two pages, so the next step would be to consider implementing a basepage which as the common property, and then inherit that from a1.aspx & ulogin.aspx.

ie

public class a1 : BasePage
{
    ...
}

public class ulogin : BasePage
{
    ...
}

public class BasePage : System.Web.Page
{
    //Put ValueToStore property here.
}

There are many users visiting same page, they may set different value, and the expected result is whatever value is set by an user on Page 1 need to be displayed in Page 2 .

Any Web technology is stateless as they use HTTP which is stateless again.

However there are many ways to get this done, each method has their own advantages.

--Session--

Please use session variable to store your value, which is a kind of variable.

Each user has different session variable to store, and its available Until the user logs out (ie till Session is available)

Storage: Server Memory

        public class Common
        {
          public int? Data
          {
             get
             {
                if(Session["Data"]!=null)
                {
                    return  int.Parse(Session["Data"].ToString());
                }
                return null.
             }
             set
             {
               Session["Data"]=value;
             }
          }

        }

--Query String--

You can pass value from one page to another page using query string.

Page 1

int value=1;
Response.Redirect("Page2.aspx?data="+value.ToString())

Page 2

if(!string.IsNullOrEmpty(Request["data"]))
{
int value=int.Parse(Request["data"]);
}

--Posting--

You can also post the value from one page to another page.

Page 1 (html)

<form action="page2.aspx" method="post">
<input type="hidden" name="data" value="1"/> 
</form>

Page 2

if(!string.IsNullOrEmpty(Request["data"]))
{
 int value=int.Parse(Request["data"]);
}

There are even more ways... You have to select what is suitable for your scenario.

Read ASP.NET State management

http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx

If the page ulogin.aspx is designed to be always redirected from a1.aspx, then set the PreviousPageType in ulogin.aspx and get the previous page values by this.PreviousPage instance. (Cross-Page-PostBack)

Convert member v to a property of common. Store common into a Session variable. And once you are ready to get the value, cast session variable to common and access v property from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM