I have a C# Web application that has 1 Web form that has 2 command buttons on
it with a label for output. I have a form level variable declared String
strName = "Jim"; right below where the buttons are declared by the form
designer. I have two event methods for my 2 command buttons. In the
Button1_Click event method I change the value of the form level variable
strName = "Kevin";. In the second event method, Button2_Click I check to see
if strName == "Jim", if so the label displays "failure", but if strName ==
"Kevin" the label displays "success".
Now, when I click on Button1, then click on Button2 the label displays
"failure". Why is my form level variable getting cleared out on the post
back? I have three different applications that are showing the same
symptoms. Please, can somebody shed some light? 2 3617
Hi,
That is the inherent nature of Internet applications, they are stateless. So
whatever variables you're setting at the form level, are recreated at each
postbacks with default values. If you need the variable to persist across
postbacks, consider adding it to Viewstate so that you can retrieve it
later.
regards
Joyjit
"Jim" <Ji*@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com... I have a C# Web application that has 1 Web form that has 2 command buttons
on it with a label for output. I have a form level variable declared String strName = "Jim"; right below where the buttons are declared by the form designer. I have two event methods for my 2 command buttons. In the Button1_Click event method I change the value of the form level variable strName = "Kevin";. In the second event method, Button2_Click I check to
see if strName == "Jim", if so the label displays "failure", but if strName == "Kevin" the label displays "success".
Now, when I click on Button1, then click on Button2 the label displays "failure". Why is my form level variable getting cleared out on the post back? I have three different applications that are showing the same symptoms. Please, can somebody shed some light?
That's just how it works.
If you want to store values between postbacks you need to store the value
manually.
In this case it sounds like you want a page scoped variable, so ViewState
would likely be your best option.
In Button1's click event put code like this:
ViewState("Name")="Jim"
Then in button2's event put code like this:
If ViewState("Name").ToString = "Kevin" then 'display success
You could use Session scope (with similar syntax) if you'd like this value
to be visible to other pages for that user.
--
I hope this helps,
Steve C. Orr, MCSD, MVP http://Steve.Orr.net
"Jim" <Ji*@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com... I have a C# Web application that has 1 Web form that has 2 command buttons on it with a label for output. I have a form level variable declared String strName = "Jim"; right below where the buttons are declared by the form designer. I have two event methods for my 2 command buttons. In the Button1_Click event method I change the value of the form level variable strName = "Kevin";. In the second event method, Button2_Click I check to see if strName == "Jim", if so the label displays "failure", but if strName == "Kevin" the label displays "success".
Now, when I click on Button1, then click on Button2 the label displays "failure". Why is my form level variable getting cleared out on the post back? I have three different applications that are showing the same symptoms. Please, can somebody shed some light? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Matthews |
last post by:
Hi,
While coding programs, I cam about a conundrum regarding
variables defined in an iterative loop.
The issue is whether it is more efficient to factor the
definition out of the loop or...
|
by: Thomas Matthews |
last post by:
Hi,
I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).
class Book
{...
|
by: lawrence |
last post by:
If I'm pretty sure there is just one form on the page, can i do this?
var myForm = document.forms;
If I'm not sure about the form, is it safer to do this?
if (document.forms) {
var myForm =...
|
by: Stuart Perryman |
last post by:
Hi,
I have the following code which works just fine in IE6 but not in
Firefox.
It is an extract of several table rows each with an individual form.
It is generated by php.
<form...
|
by: (Pete Cresswell) |
last post by:
I know I can open many instances of a given form, but I've never done it.
Now I'm analyzing an application where that seems like just the ticket: Many
investment funds, *lots* of data points for...
|
by: ElanKathir .S.N |
last post by:
Hi all !
VB.NET adds the ability to create variables that are visible only within
a block. A block is any section of code that ends with one of the words
End , Loop , or Next . This
means that...
|
by: Jody Gelowitz |
last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code works fine. However, when using VS.NET 2.0...
|
by: jedimasta |
last post by:
Good evening all,
I'm a relatively new to javascript, but I've been working with
ColdFusion and PHP for years so I'm not necessarily ignorant, just
stuck and frustrated. Using ColdFusion I'm...
|
by: Rob |
last post by:
Let's say you open Form1 that contains TabControl1
There are several tabs on TabControl1
Now you open a new Form2 that contains a User Control
How can you determine the Selected tab in Form1...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |