Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c# - Passing values between separate classes in ASP.NET and storing them in the initial class

I'm building a website in ASP.NET WebForm and I want to use a static type variable to hold data and use it in different classes.

To avoid the variable to reset, I declared the variable in a static type class, like this:

public static class Variavel
    {
        static int[] array = new int[30];
    }

I would like suggestions on how to use the variable in other classes particularly in classes like public partial class WebForm1 : System.Web.UI.Page without losing the data.

question from:https://stackoverflow.com/questions/65903966/passing-values-between-separate-classes-in-asp-net-and-storing-them-in-the-initi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

if you want to store data in web forms, I suggest you use a session. If you use static, you will probably have big problems when your app going to be used, so use static only for global and constant information and not variable data. To use sessions in web forms you can do that like this example:

//textbox1 and textbox2 are controls of webform
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;

Response.Redirect("form2.aspx");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...