开发者

This must be simple - ASP Global Variable

Ok, I've been stuck on this for a while now and it must be so simple. Cant find exact answr via google but it must be there.

I'm new to Web Apps (reasonably new to dev).

I have a web form with a text box on and a button - very simples.. On button_click, this calls multiple methods from other class开发者_运维问答es, in these other classes, I need to know the value of the text that was entered in the textbox. I could pass this value around with each new class instantiation - is this the only way to do it? or how can I create a global variable that the other classes can just access (i.e. string test = _default.mystring)

I cant create a static property. I'm sure this is something rather basic but cant seem to get my gead around it.

thanks


You can do one thing is pass the value as parameter to the function of other class

or

make use of session variable to gain access.


The "correct" way to do this would be to pass the string around as arguments to the methods that need it - either in the constructor of objects, or (more commonly practiced) as parameters to the methods where it's needed.

There is no way to create a global variable without creating a static property on some class (the class can also be static), since .NET is inherently object oriented, and there is no .NET code that isn't contained in a class.


A property should work fine:

private static string _text;

public static string Text
{
 get {return _text;}
 set {text = value;}
}


Store user specific data in session variables and application specific data in application variables. You can access these variables on every page of your application Session["key"]="value";

Application["key"]="value";

On accessing variables type cast the variable to the exact data type. IF Session contains integer value.

Session["1"]=34; int a = int.Parse(Session["1"].ToString());

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜