简体   繁体   English

使用一个aspx.cs文件中的值到另一个具有公共类的aspx.cs文件

[英]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 公共类文件common.cs :这个文件,我通过单击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: 另一个文件:它是一个网页文件名是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 文本框中的值存储在cv变量中

So, now, I want the value which was given in the textbox1.text in another webpage file named as ulogin.aspx.cs 那么,现在,我想在另一个名为ulogin.aspx.cs的网页文件中的textbox1.text中给出的值

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..... 但运行后显示值为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. 在Web应用程序,你需要坚持(通常是信息的地方常见的Session每用户信息或Application的每个应用程序的信息),以便它可以在应用程序的不同页面和用户控件之间使用。

I'd suggest adding a Session backed property to your page & usercontrol which accesses a common Session["variable"]. 我建议在你的页面和usercontrol中添加一个Session支持的属性,它访问一个公共的Session [“variable”]。 Something like the following. 像下面这样的东西。

(ie lets imagine your code was being exectued on a button click) (即让我们想象您的代码是在点击按钮时被激活的)

a1.aspx.cs 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 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. 如您所见,您现在在两个页面之间存在一些代码重复,因此下一步将考虑实现作为公共属性的基页,然后从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 . 有许多用户访问同一页面,可以设置不同的值,预期的结果是什么值由1需要的用户设置要显示在第2页

Any Web technology is stateless as they use HTTP which is stateless again. 任何Web技术都是无状态的,因为它们使用的HTTP再次是无状态的。

However there are many ways to get this done, each method has their own advantages. 然而,有很多方法可以完成这项工作,每种方法都有各自的优势。

--Session-- --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 第1页

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

Page 2 第2页

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

--Posting-- --Posting--

You can also post the value from one page to another page. 您还可以将值从一个页面发布到另一个页面。

Page 1 (html) 第1页(html)

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

Page 2 第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 阅读ASP.NET状态管理

http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx 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. 如果页面ulogin.aspx被设计为始终从a1.aspx重定向,则在ulogin.aspx中设置PreviousPageType并通过this.PreviousPage实例获取以前的页面值。 (Cross-Page-PostBack) (跨页回发)

Convert member v to a property of common. 将成员v转换为common的属性。 Store common into a Session variable. 将common存储到Session变量中。 And once you are ready to get the value, cast session variable to common and access v property from there. 一旦你准备好获取值,将会话变量转换为common并从那里访问v属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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