473,587 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable scope problem?

Tom
Hi

I have the following code and why can't i change the value of myvar in the
DataGrid1_editc ommand event so that the (changed) value of myvar is there in
the Update event?? (it fires i debugged it!):

namespace MYproject

{

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.W ebControls;

using System.Web.UI.H tmlControls;

public class MyUsercontrol: System.Web.UI.U serControl

{

public string myvar ="hello";

private void Page_Load(objec t sender, System.EventArg s e)

{
}
public void DataGrid1_EditC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

{
myvar ="goodbye"; //enters this code first
}

private void DataGrid1_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

{

string temp; // enters this code next!!!

temp = myvar; // myvar = "hello" ???? instead of the wanted "goodbye"
}

}

}

ch Tom
Nov 16 '05 #1
3 981
The variable value will be lost between postbacks. If it need to be
persisted it should be saved to teh view state by the control and got back
when a postback occurs.

Ciaran
"Tom" <to**********@h otmail.com> wrote in message
news:eK******** ******@TK2MSFTN GP10.phx.gbl...
Hi

I have the following code and why can't i change the value of myvar in the
DataGrid1_editc ommand event so that the (changed) value of myvar is there
in
the Update event?? (it fires i debugged it!):

namespace MYproject

{

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.W ebControls;

using System.Web.UI.H tmlControls;

public class MyUsercontrol: System.Web.UI.U serControl

{

public string myvar ="hello";

private void Page_Load(objec t sender, System.EventArg s e)

{
}
public void DataGrid1_EditC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

{
myvar ="goodbye"; //enters this code first
}

private void DataGrid1_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

{

string temp; // enters this code next!!!

temp = myvar; // myvar = "hello" ???? instead of the wanted "goodbye"
}

}

}

ch Tom

Nov 16 '05 #2
Tom
so how do you code all this? do you have a sample for this?
"Ciaran" <ci****@theodon nells.plus.com> wrote in message
news:41******** *************** @ptn-nntp-reader04.plus.n et...
The variable value will be lost between postbacks. If it need to be
persisted it should be saved to teh view state by the control and got back
when a postback occurs.

Ciaran
"Tom" <to**********@h otmail.com> wrote in message
news:eK******** ******@TK2MSFTN GP10.phx.gbl...
Hi

I have the following code and why can't i change the value of myvar in the DataGrid1_editc ommand event so that the (changed) value of myvar is there in
the Update event?? (it fires i debugged it!):

namespace MYproject

{

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.W ebControls;

using System.Web.UI.H tmlControls;

public class MyUsercontrol: System.Web.UI.U serControl

{

public string myvar ="hello";

private void Page_Load(objec t sender, System.EventArg s e)

{
}
public void DataGrid1_EditC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

{
myvar ="goodbye"; //enters this code first
}

private void DataGrid1_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

{

string temp; // enters this code next!!!

temp = myvar; // myvar = "hello" ???? instead of the wanted "goodbye"
}

}

}

ch Tom


Nov 16 '05 #3
Hi,

You could simply save it in a session as
Session("myvar" ) = myvar;

later you retrieve it using

myvar = Session("myvar" ).ToString();

Please note that you should make sure that you saved the variable before
retrieve , usually you do so in the onload event checking for the IsPostBack
property.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Tom" <to**********@h otmail.com> wrote in message
news:Og******** ******@TK2MSFTN GP12.phx.gbl...
so how do you code all this? do you have a sample for this?
"Ciaran" <ci****@theodon nells.plus.com> wrote in message
news:41******** *************** @ptn-nntp-reader04.plus.n et...
The variable value will be lost between postbacks. If it need to be
persisted it should be saved to teh view state by the control and got
back
when a postback occurs.

Ciaran
"Tom" <to**********@h otmail.com> wrote in message
news:eK******** ******@TK2MSFTN GP10.phx.gbl...
> Hi
>
> I have the following code and why can't i change the value of myvar in the > DataGrid1_editc ommand event so that the (changed) value of myvar is there > in
> the Update event?? (it fires i debugged it!):
>
> namespace MYproject
>
> {
>
> using System;
>
> using System.Data;
>
> using System.Drawing;
>
> using System.Web;
>
> using System.Web.UI.W ebControls;
>
> using System.Web.UI.H tmlControls;
>
>
>
> public class MyUsercontrol: System.Web.UI.U serControl
>
> {
>
> public string myvar ="hello";
>
>
>
> private void Page_Load(objec t sender, System.EventArg s e)
>
> {
>
>
> }
>
>
> public void DataGrid1_EditC ommand(object source,
> System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
>
> {
>
>
> myvar ="goodbye"; //enters this code first
>
>
> }
>
>
>
> private void DataGrid1_Updat eCommand(object source,
> System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
>
> {
>
> string temp; // enters this code next!!!
>
> temp = myvar; // myvar = "hello" ???? instead of the wanted "goodbye"
>
>
> }
>
> }
>
> }
>
> ch Tom
>
>



Nov 16 '05 #4

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

Similar topics

8
2452
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the variable name for the case of non array variables. Also I want to show the array name. Is there any way that the variable name that is passed to the...
134
7831
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you...
166
8573
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
14883
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the...
6
1467
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to be accessible to the function, so I declare it having a global scope. Then I execute the function, which should echo the value of the variable....
1
25646
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that...
2
1389
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, When I try to compile the below code snippet, I get the compile error listed below in this question. In the lines below the loop, the variable j does not excist, however, trying to create it is not allowed. If it doesn't excist, why can't I create it?. I'm not looking for a solution to this problem - that's quite easy - an...
5
2227
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
112
5392
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
3
2036
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
0
7849
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...
0
8347
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...
1
7973
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...
0
8220
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5394
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.