C# asp.net 如何改变全局变量

来源:百度知道 编辑:UC知道 时间:2024/06/01 03:54:24
我想按一次按钮,type就自增加1,输出在Label上,再按一次就再自增1.
这样该怎样实现呢?请各位大虾请教。

aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"

用static int type试试

public int type
{
get{return int.Parse(ViewState["type"]);}
set{ ViewState["type"]=value;}
}
因为提交变量数据会丢失 所以要把类变量封装到视图ViewState中记录状态

把type放在Session,Cookie,或者写在页面的隐藏域里,很简单,就是页面的在加载的时候写进去,按钮点击的时候获取+1再重新存进去,具体方法很简单,好好想想,Google,百度自己解决很有乐趣的。