ASP.NET的类中private 变量得问题!(50分)

  • 主题发起人 zhengyong7381
  • 开始时间
Z

zhengyong7381

Unregistered / Unconfirmed
GUEST, unregistred user!
请大家看看这样有什么错误? 记得是ASP.NET啊! 为什么取出变量S后会丢失啊???
在页面中添加 两个Button 和 TextBox就可以了.
第一个按钮是设置值,第二个按钮是取出值,当取出是就会发现 s 又成了初始值了.
但是设置为public static 就可以了.请告诉我为什么
namespace Asset.AssetWeb.Assets
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;

private string s = &quot;sdaf&quot;;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
TextBox1.Text = s.Trim();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
s = TextBox1.Text.Trim();
}
private void Button2_Click(object sender, System.EventArgs e)
{
TextBox1.Text = s;
}
}
}
 
查看一下你的WebPage的ViewState属性是否被设置为True.
 
田伯光你好: 下面是我的页面代码,我认为已经设置对了.但是还是不行.请指示.谢谢.
<%@ Page language=&quot;c#&quot;
Codebehind=&quot;WebForm1.aspx.cs&quot;
AutoEventWireup=&quot;false&quot;
Inherits=&quot;Asset.AssetWeb.Assets.WebForm1&quot;
enableViewState=&quot;True&quot;
%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;
>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name=&quot;GENERATOR&quot;
Content=&quot;Microsoft Visual Studio .NET 7.1&quot;>
<meta name=&quot;CODE_LANGUAGE&quot;
Content=&quot;C#&quot;>
<meta name=&quot;vs_defaultClientScript&quot;
content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot;
content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;>
</HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot;
method=&quot;post&quot;
runat=&quot;server&quot;>
<FONT face=&quot;宋体&quot;>
<asp:TextBox id=&quot;TextBox1&quot;
style=&quot;Z-INDEX: 101;
LEFT: 312px;
POSITION: absolute;
TOP: 64px&quot;
runat=&quot;server&quot;
Width=&quot;320px&quot;
Height=&quot;48px&quot;></asp:TextBox>
<asp:Button id=&quot;Button1&quot;
style=&quot;Z-INDEX: 102;
LEFT: 312px;
POSITION: absolute;
TOP: 120px&quot;
runat=&quot;server&quot;
Width=&quot;96px&quot;
Height=&quot;40px&quot;
Text=&quot;设置&quot;></asp:Button>
<asp:Button id=&quot;Button2&quot;
style=&quot;Z-INDEX: 103;
LEFT: 528px;
POSITION: absolute;
TOP: 120px&quot;
runat=&quot;server&quot;
Width=&quot;104px&quot;
Height=&quot;40px&quot;
Text=&quot;获取&quot;></asp:Button></FONT>
</form>
</body>
</HTML>
 
因为你每次POST回去时服务端都会重新给s 赋值,而改为 public static 为什么可以,是因为服务端在你POST回去时没有重新对 s 赋值。看看asp.net 的运行机制你就明白了
 
summax你好,我同意你的看法,很感谢你精彩的解答,但是我不知道如果我在同一个页面中要用到一个公共变量,那有什么好的解决方法呢?
 
summax你好,五一旅游去了吗?我还在期待你圆满的解答呢,谢谢。[:)]
 
谢谢大家。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
828
SUNSTONE的Delphi笔记
S
顶部