473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ViewState/StateBag?

Consider the following code snippet:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
MsgBox(1)
ViewState("StartTime") = DateTime.Now
lblMessage.Text = "The time now is " &
ViewState("StartTime") & "<br><br>"
Else
MsgBox(2)
If (CStr(Session("dtDateTime")) <"") Then
lblMessage.Text = "<br>Your last visit: " &
Session("dtDateTime") & "<br><br>"
End If
End If
End Sub

Sub btn_Click(ByVal obj As Object, ByVal ea As EventArgs)
Dim dtDateTime As DateTime

dtDateTime = DateTime.Now
Session("dtDateTime") = dtDateTime
lblMessage.Text += "Current date time: " & dtDateTime &
"<br>Your first visit: " & ViewState("StartTime")
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Button id="submit" Text="SUBMIT" OnClick="btn_Click"
runat="server"/><br><br>
<asp:Label id="lblMessage" runat="server"/>
</form>
</body>
</html>

When the page loads for the first time, the If condition within the
Page_Load sub gets executed (the MsgBox 1 also pops up)........that's
fine but next when I click the Submit button, as expected, MsgBox 2
pops up but at the same time, the Label also shows

"The time now is " & ViewState("StartTime") & "<br><br>"

which exists in the If condition in the Page_Load sub i.e. the page
looks like

----------------
The time now is (whatever is the ViewState("StartTime") value)

Current date time: (whatever is the current datetime)
Your first visit: (whatever is the ViewState("StartTime") value)
--------------

But if the If condition under the Else condition in the Page_Load sub
is commented & the Submit button is clicked for the first time, the
page looks like this:

--------------
Your last visit:

Current date time: (whatever is the current datetime)
Your first visit: (whatever is the ViewState("StartTime") value)
--------------

Why this difference in output when the If condition under the Else
condition in the Page_Load sub is commented?

Thanks,

Arpan

Jul 24 '06 #1
1 2090
If MsgBox call is calling Windows Forms MessageBox, you have wrong way to go
about it since MsgBox is meant for Windows Forms application. not
server-side applications, where this MsgBox would be shown at the server.
FYI: http://forums.asp.net/thread/1348589.aspx (where MsgBox was used and it
also prompted as if If's had no effect), also:
http://www.syncfusion.com/faq/aspnet/search/577.aspx

I also understand that might be using this purely for testing, so therefore
replace them with something like Response.Write(1) and Response.Write(2)

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Arpan" <ar******@hotmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Consider the following code snippet:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
MsgBox(1)
ViewState("StartTime") = DateTime.Now
lblMessage.Text = "The time now is " &
ViewState("StartTime") & "<br><br>"
Else
MsgBox(2)
If (CStr(Session("dtDateTime")) <"") Then
lblMessage.Text = "<br>Your last visit: " &
Session("dtDateTime") & "<br><br>"
End If
End If
End Sub

Sub btn_Click(ByVal obj As Object, ByVal ea As EventArgs)
Dim dtDateTime As DateTime

dtDateTime = DateTime.Now
Session("dtDateTime") = dtDateTime
lblMessage.Text += "Current date time: " & dtDateTime &
"<br>Your first visit: " & ViewState("StartTime")
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Button id="submit" Text="SUBMIT" OnClick="btn_Click"
runat="server"/><br><br>
<asp:Label id="lblMessage" runat="server"/>
</form>
</body>
</html>

When the page loads for the first time, the If condition within the
Page_Load sub gets executed (the MsgBox 1 also pops up)........that's
fine but next when I click the Submit button, as expected, MsgBox 2
pops up but at the same time, the Label also shows

"The time now is " & ViewState("StartTime") & "<br><br>"

which exists in the If condition in the Page_Load sub i.e. the page
looks like

----------------
The time now is (whatever is the ViewState("StartTime") value)

Current date time: (whatever is the current datetime)
Your first visit: (whatever is the ViewState("StartTime") value)
--------------

But if the If condition under the Else condition in the Page_Load sub
is commented & the Submit button is clicked for the first time, the
page looks like this:

--------------
Your last visit:

Current date time: (whatever is the current datetime)
Your first visit: (whatever is the ViewState("StartTime") value)
--------------

Why this difference in output when the If condition under the Else
condition in the Page_Load sub is commented?

Thanks,

Arpan

Jul 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: wASP | last post by:
Hi again, I'm having a problem accessing the ViewState object. I'm using the following two functions - as copied from the MS docs - and state.count is zero in the first - and the while loop in...
10
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and...
1
by: Cristian | last post by:
Hi, I'm building a WebCustomControl, the principal class which inherits from WebControl has the property ViewState, and I want to access to the ViewState from other classes in my project, but I...
6
by: Max | last post by:
I need an vb.net class that is invoked from aspx page, that use the viewstate/session object. This class must be store the information into viewstate/session. Can you give me an example ? Thanks
5
by: Steve Richter | last post by:
In my user control I want to read the ViewState dictionary of the Parent control. But this sensible idea is not permitted by the compiler: Compiler Error Message: CS1540: Cannot access...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
9
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
2
by: epigram | last post by:
I'm responding to a button click event on an asp.net web form. I then need to retrieve the value from a TextBox control and I want to compare it against the control's previous value to see if it...
9
by: multiformity | last post by:
The following page will not load the view state from the statebag without throwing an exception. After we get this working, I will be working on viewstate for some custom controls, but one thing at...
12
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.