全局变量 的全局有多大??

有一个问题困扰我很长时间了,总是绞尽脑汁的想,也想不出所以然(当然主要是人笨)

default.aspx

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="test._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>default</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 88px"
     runat="server" AutoPostBack="True"></asp:DropDownList>
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 136px"
     runat="server"></asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 600px; POSITION: absolute; TOP: 88px" runat="server"
     Text="Button"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 232px; POSITION: absolute; TOP: 32px" runat="server"
     Width="161px" Height="24px">河北省</asp:Label></FONT></form>
  </FORM>
 </body>
</HTML>

default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
 /// <summary>
 /// _default 的摘要说明。
 /// </summary>
 public class _default : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
  protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
  protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
  protected System.Data.SqlClient.SqlConnection sqlConnection1;
  protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  protected System.Web.UI.WebControls.Button Button1;
  static string somestring;
  
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   
   if(!IsPostBack)
   {      this.sqlSelectCommand1.Connection.Open();
   
    DropDownList1.DataSource=this.sqlSelectCommand1.ExecuteReader();
    DropDownList1.DataTextField="名称";
    DropDownList1.DataValueField="代码";
    DropDownList1.DataBind();
    this.sqlSelectCommand1.Connection.Close();
   }


  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
   this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
   this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
   this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   //
   // sqlSelectCommand1
   //
   this.sqlSelectCommand1.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,2)=\'13\' and right(代码,2)=\'00\'";
   this.sqlSelectCommand1.Connection = this.sqlConnection1;
   //
   //
   // sqlSelectCommand2
   // 
   this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+somesting+"' and right(代码,2)=\'00\'";
   this.sqlSelectCommand2.Connection = this.sqlConnection1;// sqlConnection1
   //
   this.sqlConnection1.ConnectionString = "workstation id=\"THTFPC-ECEAB98C\";packet size=4096;integrated security=SSPI;data s" +
    "ource=\"THTFPC-ECEAB98C\";persist security info=True;initial catalog=Northwind";
   //
   // sqlInsertCommand1
   //
   this.sqlInsertCommand1.CommandText = "INSERT INTO 行政区划(代码, 名称) VALUES (@代码, @名称); SELECT 代码, 名称 FROM 行政区划";
   this.sqlInsertCommand1.Connection = this.sqlConnection1;
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@代码", System.Data.SqlDbType.VarChar, 6, "代码"));
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@名称", System.Data.SqlDbType.NVarChar, 15, "名称"));
   //
   // sqlDataAdapter1
   //
   this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
   this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
   this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                           new System.Data.Common.DataTableMapping("Table", "行政区划", new System.Data.Common.DataColumnMapping[] {
                                                     new System.Data.Common.DataColumnMapping("代码", "代码"),
                                                     new System.Data.Common.DataColumnMapping("名称", "名称")})});
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   somestring=DropDownList1.SelectedValue.ToString();
   Label1.Text=somestring;
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
   string tmp=somestring;
   
   this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+tmp.Substring(0,4)+"'";
   this.sqlSelectCommand2.Connection.Open();
   
   DataGrid1.DataSource=this.sqlSelectCommand2.ExecuteReader();
      
   DataGrid1.DataBind();
   this.sqlSelectCommand2.Connection.Close();
  
  }

  
 }
}

 

None.gif 数据
do{
None.gif
130000     河北省
None.gif
130100     石家庄市
None.gif
130102     长安区
None.gif
130103     桥东区
None.gif
130104     桥西区
None.gif
130105     新华区
None.gif
130107     井陉矿区
None.gif
130108     裕华区
None.gif
130121     井陉县
None.gif
130123     正定县
None.gif
130124     栾城县
None.gif
130125     行唐县
None.gif
130126     灵寿县
None.gif
130127     高邑县
None.gif
130128     深泽县
None.gif
130129     赞皇县
None.gif
130130     无极县
None.gif
130131     平山县
None.gif
130132     元氏县
None.gif
130133     赵县
None.gif
130181     辛集市
None.gif
130182     藁城市
None.gif
130183     晋州市
None.gif
130184     新乐市
None.gif
130185     鹿泉市
None.gif
130200     唐山市
None.gif
130202     路南区
None.gif
130203     路北区
None.gif
130204     古冶区
None.gif
130205     开平区
None.gif
130207     丰南区
None.gif
130208     丰润区
None.gif
130223     滦县
None.gif
130224     滦南县
None.gif
130225     乐亭县
None.gif
130227     迁西县
None.gif
130229     玉田县
None.gif
130230     唐海县
None.gif
130281     遵化市
None.gif
130283     迁安市
None.gif
130300     秦皇岛市
None.gif
130302     海港区
None.gif
130303     山海关区
None.gif
130304     北戴河区
None.gif
130321     青龙满族自治县
None.gif
130322     昌黎县
None.gif
130323     抚宁县
None.gif
130324     卢龙县
None.gif
130400     邯郸市
None.gif
130402     邯山区
None.gif
130403     丛台区
None.gif
130404     复兴区
None.gif
130406     峰峰矿区
None.gif
130421     邯郸县
None.gif
130423     临漳县
None.gif
130424     成安县
None.gif
130425     大名县
None.gif
130426     涉县
None.gif
130427     磁县
None.gif
130428     肥乡县
None.gif
130429     永年县
None.gif
130430     邱县
None.gif
130431     鸡泽县
None.gif
130432     广平县
None.gif
130433     馆陶县
None.gif
130434     魏县
None.gif
130435     曲周县
None.gif
130481     武安市
None.gif
130500     邢台市
None.gif
130502     桥东区
None.gif
130503     桥西区
None.gif
130521     邢台县
None.gif
130522     临城县
None.gif
130523     内丘县
None.gif
130524     柏乡县
None.gif
130525     隆尧县
None.gif
130526     任县
None.gif
130527     南和县
None.gif
130528     宁晋县
None.gif
130529     巨鹿县
None.gif
130530     新河县
None.gif
130531     广宗县
None.gif
130532     平乡县
None.gif
130533     威县
None.gif
130534     清河县
None.gif
130535     临西县
None.gif
130581     南宫市
None.gif
130582     沙河市
None.gif
130600     保定市
None.gif
130602     新市区
None.gif
130603     北市区
None.gif
130604     南市区
None.gif
130621     满城县
None.gif
130622     清苑县
None.gif
130623     涞水县
None.gif
130624     阜平县
None.gif
130625     徐水县
None.gif
130626     定兴县
None.gif
130627     唐县
None.gif
130628     高阳县
None.gif
130629     容城县
None.gif
130630     涞源县
None.gif
130631     望都县
None.gif
130632     安新县
None.gif
130633     易县
None.gif
130634     曲阳县
None.gif
130635     蠡县
None.gif
130636     顺平县
None.gif
130637     博野县
None.gif
130638     雄县
None.gif
130681     涿州市
None.gif
130682     定州市
None.gif
130683     安国市
None.gif
130684     高碑店市
None.gif
130700     张家口市
None.gif
130702     桥东区
None.gif
130703     桥西区
None.gif
130705     宣化区
None.gif
130706     下花园区
None.gif
130721     宣化县
None.gif
130722     张北县
None.gif
130723     康保县
None.gif
130724     沽源县
None.gif
130725     尚义县
None.gif
130726     蔚县
None.gif
130727     阳原县
None.gif
130728     怀安县
None.gif
130729     万全县
None.gif
130730     怀来县
None.gif
130731     涿鹿县
None.gif
130732     赤城县
None.gif
130733     崇礼县
None.gif
130800     承德市
None.gif
130802     双桥区
None.gif
130803     双滦区
None.gif
130804     鹰手营子矿区
None.gif
130821     承德县
None.gif
130822     兴隆县
None.gif
130823     平泉县
None.gif
130824     滦平县
None.gif
130825     隆化县
None.gif
130826     丰宁满族自治县
None.gif
130827     宽城满族自治县
None.gif
130828     围场满族蒙古族自治县
None.gif
130900     沧州市
None.gif
130902     新华区
None.gif
130903     运河区
None.gif
130921     沧县
None.gif
130922     青县
None.gif
130923     东光县
None.gif
130924     海兴县
None.gif
130925     盐山县
None.gif
130926     肃宁县
None.gif
130927     南皮县
None.gif
130928     吴桥县
None.gif
130929     献县
None.gif
130930     孟村回族自治县
None.gif
130981     泊头市
None.gif
130982     任丘市
None.gif
130983     黄骅市
None.gif
130984     河间市
None.gif
131000     廊坊市
None.gif
131002     安次区
None.gif
131003     广阳区
None.gif
131022     固安县
None.gif
131023     永清县
None.gif
131024     香河县
None.gif
131025     大城县
None.gif
131026     文安县
None.gif
131028     大厂回族自治县
None.gif
131081     霸州市
None.gif
131082     三河市
None.gif
131100     衡水市
None.gif
131102     桃城区
None.gif
131121     枣强县
None.gif
131122     武邑县
None.gif
131123     武强县
None.gif
131124     饶阳县
None.gif
131125     安平县
None.gif
131126     故城县
None.gif
131127     景县
None.gif
131128     阜城县
None.gif
131181     冀州市
None.gif
131182     深州市
}while(0)
None.gif

 

上面的程序看起来没有问题,调试的时候也看不出什么错误,因为只是我一个人访问

可是当访问的用户多了之后〉1

dataGrid中的显示数据就会不正确!

这完全是因为使用了全局变量string,至于是否static与问题似乎无关

本来想请教朋友指点,自己实际动手发现确实是somestring的问题,于是不使用全局变量使用Session

 

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//**//**//// <summary>
InBlock.gif 
/// _default 的摘要说明。
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class _default : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
protected System.Web.UI.WebControls.DropDownList DropDownList1;
InBlock.gif  
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif  
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif  
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
InBlock.gif  
protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
InBlock.gif  
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
InBlock.gif  
protected System.Data.SqlClient.SqlConnection sqlConnection1;
InBlock.gif  
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
InBlock.gif  
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif  
// static string somestring;
InBlock.gif
  
InBlock.gif 
InBlock.gif  
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
// 在此处放置用户代码以初始化页面
InBlock.gif
   
InBlock.gif   
if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{ Session.Add("somesting","qqq");
InBlock.gif    
this.sqlSelectCommand1.Connection.Open();
InBlock.gif   
InBlock.gif    DropDownList1.DataSource
=this.sqlSelectCommand1.ExecuteReader();
InBlock.gif    DropDownList1.DataTextField
="名称";
InBlock.gif    DropDownList1.DataValueField
="代码";
InBlock.gif    DropDownList1.DataBind();
InBlock.gif    
this.sqlSelectCommand1.Connection.Close();
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  Web 窗体设计器生成的代码
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif  
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//
InBlock.gif   
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif   
//
InBlock.gif
   InitializeComponent();
InBlock.gif   
base.OnInit(e);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//**//**//// <summary>
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{    
InBlock.gif   
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif   
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif   
this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
InBlock.gif   
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif   
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
InBlock.gif   
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
InBlock.gif   
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif   
// 
InBlock.gif   
// sqlSelectCommand1
InBlock.gif   
// 
InBlock.gif
   this.sqlSelectCommand1.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,2)=\'13\' and right(代码,2)=\'00\'";
InBlock.gif   
this.sqlSelectCommand1.Connection = this.sqlConnection1;
InBlock.gif   
// 
InBlock.gif   
// 
InBlock.gif   
// sqlSelectCommand2
InBlock.gif   
// 
InBlock.gif   
//this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+somesting+"' and right(代码,2)=\'00\'";
InBlock.gif
   this.sqlSelectCommand2.Connection = this.sqlConnection1;// sqlConnection1
InBlock.gif   
// 
InBlock.gif
   this.sqlConnection1.ConnectionString = "workstation id=\"THTFPC-ECEAB98C\";packet size=4096;integrated security=SSPI;data s" +
InBlock.gif    
"ource=\"THTFPC-ECEAB98C\";persist security info=True;initial catalog=Northwind";
InBlock.gif   
// 
InBlock.gif   
// sqlInsertCommand1
InBlock.gif   
// 
InBlock.gif
   this.sqlInsertCommand1.CommandText = "INSERT INTO 行政区划(代码, 名称) VALUES (@代码, @名称); SELECT 代码, 名称 FROM 行政区划";
InBlock.gif   
this.sqlInsertCommand1.Connection = this.sqlConnection1;
InBlock.gif   
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@代码", System.Data.SqlDbType.VarChar, 6"代码"));
InBlock.gif   
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@名称", System.Data.SqlDbType.NVarChar, 15"名称"));
InBlock.gif   
// 
InBlock.gif   
// sqlDataAdapter1
InBlock.gif   
// 
InBlock.gif
   this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
InBlock.gif   
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
ExpandedSubBlockStart.gifContractedSubBlock.gif   
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                           
new System.Data.Common.DataTableMapping("Table""行政区划"new System.Data.Common.DataColumnMapping[] dot.gif{
InBlock.gif                                                     
new System.Data.Common.DataColumnMapping("代码""代码"),
ExpandedSubBlockEnd.gif                                                     
new System.Data.Common.DataColumnMapping("名称""名称")}
)}
);
InBlock.gif   
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
InBlock.gif  
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   Session[
"somesting"]=DropDownList1.SelectedValue.ToString();
InBlock.gif   Label1.Text
=Session["somesting"].ToString();
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string tmp=Session["somesting"].ToString();
InBlock.gif   
InBlock.gif   
this.sqlSelectCommand2.CommandText = "SELECT 代码, 名称 FROM 行政区划 where left(代码,4)='"+tmp.Substring(0,4)+"'";
InBlock.gif   
this.sqlSelectCommand2.Connection.Open();
InBlock.gif   
InBlock.gif   DataGrid1.DataSource
=this.sqlSelectCommand2.ExecuteReader();
InBlock.gif      
InBlock.gif   DataGrid1.DataBind();
InBlock.gif   
this.sqlSelectCommand2.Connection.Close();
InBlock.gif  
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

问题的测试好像已经结束,各用户之间不再互相影响

问题还要到单位,看看自己的程序再说

转载于:https://www.cnblogs.com/henry_zjk/archive/2005/01/19/93898.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值