473,511 Members | 16,830 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_editcommand 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.WebControls;

using System.Web.UI.HtmlControls;

public class MyUsercontrol: System.Web.UI.UserControl

{

public string myvar ="hello";

private void Page_Load(object sender, System.EventArgs e)

{
}
public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

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

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

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

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

}

}

ch Tom
Nov 16 '05 #1
3 976
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**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
Hi

I have the following code and why can't i change the value of myvar in the
DataGrid1_editcommand 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.WebControls;

using System.Web.UI.HtmlControls;

public class MyUsercontrol: System.Web.UI.UserControl

{

public string myvar ="hello";

private void Page_Load(object sender, System.EventArgs e)

{
}
public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

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

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs 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****@theodonnells.plus.com> wrote in message
news:41***********************@ptn-nntp-reader04.plus.net...
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**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
Hi

I have the following code and why can't i change the value of myvar in the DataGrid1_editcommand 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.WebControls;

using System.Web.UI.HtmlControls;

public class MyUsercontrol: System.Web.UI.UserControl

{

public string myvar ="hello";

private void Page_Load(object sender, System.EventArgs e)

{
}
public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

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

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs 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**********@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
so how do you code all this? do you have a sample for this?
"Ciaran" <ci****@theodonnells.plus.com> wrote in message
news:41***********************@ptn-nntp-reader04.plus.net...
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**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
> Hi
>
> I have the following code and why can't i change the value of myvar in the > DataGrid1_editcommand 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.WebControls;
>
> using System.Web.UI.HtmlControls;
>
>
>
> public class MyUsercontrol: System.Web.UI.UserControl
>
> {
>
> public string myvar ="hello";
>
>
>
> private void Page_Load(object sender, System.EventArgs e)
>
> {
>
>
> }
>
>
> public void DataGrid1_EditCommand(object source,
> System.Web.UI.WebControls.DataGridCommandEventArgs e)
>
> {
>
>
> myvar ="goodbye"; //enters this code first
>
>
> }
>
>
>
> private void DataGrid1_UpdateCommand(object source,
> System.Web.UI.WebControls.DataGridCommandEventArgs 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
2446
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...
134
7749
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...
166
8480
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
14867
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...
6
1457
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...
1
25621
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...
2
1384
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...
5
2221
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
5350
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...
3
2028
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...
0
7242
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,...
0
7138
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...
0
7355
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,...
1
7081
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...
0
7510
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...
0
5668
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,...
1
5066
isladogs
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...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.