如何申明全局變量?

ny_nicholas 2004-08-18 04:50:45
如下所示(boolSort的值總是會被重置,怎樣才能保存下來,急
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents lbJe As System.Web.UI.WebControls.Label
Protected WithEvents ddlJE As System.Web.UI.WebControls.DropDownList
Protected WithEvents dgLine As System.Web.UI.WebControls.DataGrid
Protected WithEvents calDate As System.Web.UI.WebControls.Calendar
Protected WithEvents dgTotal As System.Web.UI.WebControls.DataGrid

Dim boolSort As Boolean = False
Dim strWhereCause As String = ""
Dim strCause As String = ""
Dim strViewString As String = ""
Dim strLine As String = ""
Dim strTotal As String = "select workid as 工單號, Count(je) as 數量 from duke group by workid"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub


'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
calDate.SelectedDate = Now
If Not Page.IsPostBack Then
strLine = "select WorkID,je,Count(je) from duke where datediff(day,getdate(),sndt)=0"
strLine &= " group by WorkID,Je Order by Je"
strViewString = "WorkID"
boolSort = False
setdata()
End If
End Sub

Sub ChangeFilter(ByVal Source As Object, ByVal E As EventArgs)
strWhereCause = ""
strWhereCause = "(datediff(day,'" & calDate.SelectedDate.ToString() & "',sndt)=0)"
FilterJe(ddlJE.SelectedItem.Text)
strWhereCause = " where " & strWhereCause & " AND " & strCause

strLine = "Select WorkID,Je,Count(je) From duke " & strWhereCause
strLine &= " group by WorkID,Je Order by Je"
boolSort = True
setdata()

End Sub

Sub FilterJe(ByVal strChoice As String)
strCause = ""
Select Case strChoice
Case "All"
strCause = "(Je<>0)"
Case "1"
strCause = "(Je=1)"
Case "2"
strCause = "(Je=2)"
Case "3"
strCause = "(Je=3)"
Case "4"
strCause = "(Je=4)"
Case "5"
strCause = "(Je=5)"
Case "6"
strCause = "(Je=6)"
Case "7"
strCause = "(Je=7)"
Case "8"
strCause = "(Je=8)"
Case "9"
strCause = "(Je=9)"
Case "10"
strCause = "(Je=10)"
End Select
End Sub

Sub SorColumn(ByVal Source As Object, ByVal E As DataGridSortCommandEventArgs)
If boolSort Then
strLine = "Select WorkID,Je,Count(je) From duke " & strWhereCause
strLine &= " group by WorkID,Je Order by Je"
'Response.Write(strLine)
Else
strLine = "select WorkID,je,Count(je) from duke where datediff(day,getdate(),sndt)=0"
strLine &= " group by WorkID,Je Order by Je"
End If
'Response.Write(strLine)
strViewString = E.SortExpression
setdata()
End Sub

Sub setdata()
Dim strConn As String = "server=192.168.203.21;database=mfd;uid=mfd;pwd=mfd"
Dim objConn As New SqlConnection(strConn)
'Response.Write(strLine)
Dim objAdapter As New SqlDataAdapter(strLine, objConn)

Dim objDataSet As New DataSet
objAdapter.Fill(objDataSet, "dtLine")
Dim dtline As DataTable = objDataSet.Tables("dtLine")

Dim dvView As New DataView(dtline)
dvView.Sort = strViewString

dgLine.DataSource = dvView
dgLine.DataBind()

Dim objAdapter1 As New SqlDataAdapter(strTotal, objConn)
objAdapter1.Fill(objDataSet, "dtWorkID")
Dim dtWorkID As DataTable = objDataSet.Tables("dtWorkID")
Dim dvView1 As New DataView(dtWorkID)
dgTotal.DataSource = dvView1
dgTotal.DataBind()

End Sub

End Class
...全文
505 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingnt 2004-08-19
  • 打赏
  • 举报
回复
一个页面里面使用的话用ViewState比较好!不同页面使用则Session比较方便!
hbb0b0 2004-08-19
  • 打赏
  • 举报
回复
把变量的域定义在全局,比如Application或session.
lglesias 2004-08-19
  • 打赏
  • 举报
回复
对啊,
为什么不用session呢?
ny_nicholas 2004-08-19
  • 打赏
  • 举报
回复
各位能不能說具體些,舉個例子說一下呀
lwbmail 2004-08-19
  • 打赏
  • 举报
回复
在ASP.NET里,用Shared来定义全局变量
ycc2008 2004-08-18
  • 打赏
  • 举报
回复
用get,set属性
极限999 2004-08-18
  • 打赏
  • 举报
回复
建议用ViewState对象用法和session差不多。
你可以在合适的方法中恢复值和改变值。
youngfox 2004-08-18
  • 打赏
  • 举报
回复
建议用Session
myxs 2004-08-18
  • 打赏
  • 举报
回复
ASP.NET中无WINFORM中的静态变量
bestxjp 2004-08-18
  • 打赏
  • 举报
回复
一个页面里面使用的话用ViewState比较好!不同页面使用则Session比较方便!
peering08cn 2004-08-18
  • 打赏
  • 举报
回复
用session或者viewstate 这两个对象
brightheroes 2004-08-18
  • 打赏
  • 举报
回复
Dim boolSort As Boolean = False
---用ViewState来保存变量的信息
在ASP.NET里面,当你点击BUTTON之类的服务器端控件会引起页面POSTBACK,这个时候页面重新生成,当然变量会丢失
所以要用ViewState来记录变量
exboy 2004-08-18
  • 打赏
  • 举报
回复
把变量声明为静态变量,VB.NET里面好像叫做共享(shared)变量
redebug 2004-08-18
  • 打赏
  • 举报
回复
为什么不用session呢?
exboy 2004-08-18
  • 打赏
  • 举报
回复
shared Dim boolSort As Boolean = False

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧