473,763 Members | 6,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form level variable scope issue

Jim
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?
Nov 19 '05 #1
2 3633
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*@discussion s.microsoft.com > wrote in message
news:94******** *************** ***********@mic rosoft.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?

Nov 19 '05 #2
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*@discussion s.microsoft.com > wrote in message
news:94******** *************** ***********@mic rosoft.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?

Nov 19 '05 #3

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

Similar topics

3
2869
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 maintain encapsulation by leaving it inside the loop? Common stuff for examples: class Data;
2
8832
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 { public: Book()
18
8006
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 = document.forms; // more code here........ }
4
9301
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 action="MaintNotification.php?ReqID=5" method="post" name="frm5"> <tr align="left" bgcolor="#dddddd" class="text" onClick="submit()"
12
3237
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 each fund, and a desire by the users to see several funds presented side-by-side. Is opening, say, five instances of the same form real-world-doable? -- PeteCresswell
10
3827
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 For...Next and If...End If blocks can have their own variables. So,
6
14219
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 (XslCompiledTransform), the exact same XSLT transformation fails with the error: System.Xml.Xsl.XslLoadException: The variable or parameter 'lastrecordwaskit' was duplicated within the same scope. An error occurred at (174,12). at...
4
7267
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 using an include to pull in form elements (text fields, checkboxes, etc...) multiple times on a single page. The included page does not have a form tag of it's own, but the root page has uniquely named forms for validation. Imagine it like this:
12
2370
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 from the User Control in Form2 ? Hope this is clear enough...
0
9564
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.