Scope discussion of variables in .NET

xiaoxiao2021-03-06  49

I have always used .NET to do C / S mode development, recently use .NET to do B / S mode development. I found that the value of variables defined in WebForm will be lost, where is it there?

I did some tests, I used WebControl on the Webform, when I do some operations to let the page submit the server, this time, things are gone.

Here is my test, I hope the expert guidance:

1. I put two TextBox and two button on the page, named TB_1, TB_2, BTN_1, BTN_2, respectively.

II. I define two variables on the page.

Private Stirng Str1 = "";

Private static string str2 = "";

Third. Give them the value when the page is loaded, I just want to do it during the first time.

IF (! this.ispostback)

{

STR1 = "1";

STR2 = "2";

}

IV. I want to use the value of the variable when I point the button.

BTN_1_Click ----> this.tb_1.text = str1;

BTN_2_Click ----> this.tb_2.text = STR2;

5. How is the result?

The first no thing, the second can get a value.

why?

Is it as long as the page refreshes, all member variables are redefined?

The value of the static variable is retained. Is it necessary to use a static variable if it is to achieve this?

I don't know if this is the right solution, I hope the expert advice. :)

Contact: denny_lzh@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-116091.html

New Post(0)