Global Variable


S

Shapper

Hello,

I am declaring a variable in my aspx.vb code as follows:

Public Class catalogue
Public productid As String
Private Sub Page_Load
...

I have an image button where I call the following function:

Public Sub MyFunction(ByVal sender As Object, ByVal e As
CommandEventArgs)

Response.Write(productid)
Response.Write(e.CommandName)

End Sub

I can see the e.CommandName value but not the productid.

Is not productid global the way I declare it?

Thanks,
Miguel
 
Ad

Advertisements

T

Tom.PesterDELETETHISSS

If you use the productid in the same class you should see it. Post a small
workable sample so we can repro and Ill get abck to you.

Are you also sure productid has a value since you dont post about an error
being thrown ? ;)

Cheers,
Tom Pester
 
P

Patrice

1) Keep in mind that ASP.NET pages are created and disposed each time there
is an HTTP request i.e. members won't persist from one HTTP request to the
next (as this is not the same object that will handle both requests)...

2) If you already know this, make sure you set productid before using it.
For example it could be because events are not happening in the order you
expect. How is the "ProductID" member intialized ?
 
K

Kevin Spencer

I can see the e.CommandName value but not the productid.

Where can you see it?

Also, you just might want to inherit System.Web.UI.Page in your class if you
expect any System.Web.UI.Page events (such as Page_Load) to fire.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
S

Shapper

Hello,

I will try to describe my problem.

In my page I have 2 distinct regions:

1. Datagrid which show a list of all products.

2. Region with 4 ASP Labels, 1 ASP Image and 3 ASP Image Buttons.
This will show the detailed information of a certain product.

So when I choose a datagrid record the function Product_Detail is called
and the Labels, Image and Image Buttons are filled with the right
productid.

I mentioned I have 3 ImageButtons in region 2 of the page. Why?
These buttons show image thumbnails and when clicked call the function
Zoom_Image which will show the right image in the ASP:Image.

When Zoom_Image runs it needs to know which productid is current used in
region 2. So everytime I click my datagrid I set the global variable
currentproductid=productid clicked.

This way I when I press one of the image buttons the zoom function knows
which record is in use.

Does this make any sense?

Somewhere here the global variable currentproductid.

Maybe my approach is wrong.

Any idea?

Thanks,
Miguel
 
K

Kevin Spencer

Hi Miguel,

I'm still confused. Sorry. First, what exactly do you mean by "region?" In
Visual Studio.Net, a region is simply an organizational tool for use in
development, and has nothing to do with how an app works. In an ASP.Net
page, I have no idea what the word is supposed to represent.

Second, as I mentioned before, I saw some of your code, which defined a
class called "catalogue." Here is a reproduction of the code you posted
initially:

Now, the Page_Load Sub leads me to believe that this class is somehow
supposed to be derived from System.Web.UI.Page, but there is no "Inherits"
statement that indicates that it does. So, part of my confusion extends from
that.

After the ellipse, you state:

I'm not at all sure of what this class has to do with your Page, if
anything. The variable (field) "productid" is a public field of the
"catalogue" class. So, since I don't know what's going on with regards to
that class, I can't figure out what exactly is going wrong.

I don't know where you "have" this image button. If it is a member of the
"catalogue" class, what again does it have to do with an ASP.Net Page class,
which you have talked about, but doesn't seem to appear anywhere in your
code?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Ad

Advertisements

S

Shapper

Hi,

Yes, I have to admit my explanation was confusing.

1. All the functions are in Class Catalogue.
2. Yes, class catalogue is derived from Inherits System.Web.UI.Page.
The class is in catalogue.aspx.vb. It has the code for
catalogue.aspx.

3. In my aspx page (HTML code) I have 2 div's:

A) DIV id="Products_Grid"

DIV Products_Grid includes PRODUCTS datagrid which shows N
records.

The DataGrid has 3 database fields:
1 - [Image01]
2 - [Title]
3 - [Price]

B) DIV id="Product_Detail"

In DIV Product_Detail I have:
1 - One ASP:Image - [Image01] database field by default. Full
Size.
2 - Three ASP:ImageButtons - 3 thumbnail images using the fields:
[Image01], [Image02], [Image03]
3 - Four ASP:Labels - [Title], [Description], [Price],
Code:
4. My functions

A) Build_Grid()
Builds the PRODUCTS datagrid, binds it to data...

B) Change_Product()
When a record in datagrid is pressed it changes all the ASP:Image,
ASP:ImageButtons and ASP:Labels in DIV Product_Detail.

Like a Master-Detail in the same page.

C) Change_Photo()
When an ASP:ImageButton is pressed it gets its value and change
the
URL of full size image displayed in ASP:Image in DIV
Product_Detail.

This is where I have the problem!

Change_Photo() receives the field to use from the ASP:ImageButton.
I am using e.CommandName.

Q: But what record is being displayed? What is its productid?
A: It is the same as last record pressed in the datagrid.


So my implementation is:
[QUOTE]
Change_Product() is called THEN:[/QUOTE]

Global variable CurrentProductID = ProductID of row pressed in
datagrid.
Change Product_Detail Labels and Images values using this record
value.
[QUOTE]
Change_Photo() is called THEN[/QUOTE]
Get new Thumbnail to show in ASP:Image (full size) from
e.CommmandName
Get CurrentProductID
Change ASP:Image "Photo" using the 2 values.

Well, this might be confusing but it's nothing more then a master-detail
page in some page and in the detail part I have 3 thumbnails with a full
size feature for them.

Everything is working but when I click the image buttons it's like the
global variable CurrentProductID is not available. I have no idea why.

Thanks,
Miguel
 
K

Kevin Spencer

Thanks Miguel,

I think it's getting a lot clearer.

I'm a little fuzzy on what the exact sequence of PostBacks and data changing
is. Something changes the value of CurrentProductId, I think. But what
persists that value across PostBacks? Remember that a Page class lasts for
the time it takes to process a Request. Afterwards, the class is destroyed,
and rebuilt with the next Request. That is why ViewState exists, to restore
the values of Controls after a PostBack. But I can't tell for sure whether
the value is set during one PostBack and read in another, or how you are
persisting this value across PostBacks.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

Shapper said:
Hi,

Yes, I have to admit my explanation was confusing.

1. All the functions are in Class Catalogue.
2. Yes, class catalogue is derived from Inherits System.Web.UI.Page.
The class is in catalogue.aspx.vb. It has the code for catalogue.aspx.

3. In my aspx page (HTML code) I have 2 div's:

A) DIV id="Products_Grid"

DIV Products_Grid includes PRODUCTS datagrid which shows N records.

The DataGrid has 3 database fields:
1 - [Image01]
2 - [Title]
3 - [Price]

B) DIV id="Product_Detail"

In DIV Product_Detail I have:
1 - One ASP:Image - [Image01] database field by default. Full Size.
2 - Three ASP:ImageButtons - 3 thumbnail images using the fields:
[Image01], [Image02], [Image03]
3 - Four ASP:Labels - [Title], [Description], [Price],
Code:
4. My functions

A) Build_Grid()
Builds the PRODUCTS datagrid, binds it to data...

B) Change_Product()
When a record in datagrid is pressed it changes all the ASP:Image,
ASP:ImageButtons and ASP:Labels in DIV Product_Detail.

Like a Master-Detail in the same page.

C) Change_Photo()
When an ASP:ImageButton is pressed it gets its value and change the
URL of full size image displayed in ASP:Image in DIV Product_Detail.

This is where I have the problem!

Change_Photo() receives the field to use from the ASP:ImageButton.
I am using e.CommandName.

Q: But what record is being displayed? What is its productid?
A: It is the same as last record pressed in the datagrid.


So my implementation is:
[QUOTE]
Change_Product() is called THEN:[/QUOTE]

Global variable CurrentProductID = ProductID of row pressed in datagrid.
Change Product_Detail Labels and Images values using this record value.
[QUOTE]
Change_Photo() is called THEN[/QUOTE]
Get new Thumbnail to show in ASP:Image (full size) from e.CommmandName
Get CurrentProductID
Change ASP:Image "Photo" using the 2 values.

Well, this might be confusing but it's nothing more then a master-detail
page in some page and in the detail part I have 3 thumbnails with a full
size feature for them.

Everything is working but when I click the image buttons it's like the
global variable CurrentProductID is not available. I have no idea why.

Thanks,
Miguel
















[QUOTE]
Hi Miguel,

I'm still confused. Sorry. First, what exactly do you mean by "region?"
In
Visual Studio.Net, a region is simply an organizational tool for use in
development, and has nothing to do with how an app works. In an ASP.Net
page, I have no idea what the word is supposed to represent.

Second, as I mentioned before, I saw some of your code, which defined a
class called "catalogue." Here is a reproduction of the code you posted
initially:




Now, the Page_Load Sub leads me to believe that this class is somehow
supposed to be derived from System.Web.UI.Page, but there is no
"Inherits"
statement that indicates that it does. So, part of my confusion extends
from
that.

After the ellipse, you state:




I'm not at all sure of what this class has to do with your Page, if
anything. The variable (field) "productid" is a public field of the
"catalogue" class. So, since I don't know what's going on with regards to
that class, I can't figure out what exactly is going wrong.

I don't know where you "have" this image button. If it is a member of the
"catalogue" class, what again does it have to do with an ASP.Net Page
class,
which you have talked about, but doesn't seem to appear anywhere in your
code?

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.
[/QUOTE]
[/QUOTE]
 
S

Shapper

Got lost. :)

Well, I made this work using a Session Variable which holds the
productid value which is in use in the Detail Page.

The Session Value is reset only when Page_Load.

Does this allows to take any conclusion of what was happening when I was
using the global variable?

Cheers,
Miguel

Thanks Miguel,

I think it's getting a lot clearer.

I'm a little fuzzy on what the exact sequence of PostBacks and data changing
is. Something changes the value of CurrentProductId, I think. But what
persists that value across PostBacks? Remember that a Page class lasts for
the time it takes to process a Request. Afterwards, the class is destroyed,
and rebuilt with the next Request. That is why ViewState exists, to restore
the values of Controls after a PostBack. But I can't tell for sure whether
the value is set during one PostBack and read in another, or how you are
persisting this value across PostBacks.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

Hi,

Yes, I have to admit my explanation was confusing.

1. All the functions are in Class Catalogue.
2. Yes, class catalogue is derived from Inherits System.Web.UI.Page.
The class is in catalogue.aspx.vb. It has the code for catalogue.aspx.

3. In my aspx page (HTML code) I have 2 div's:

A) DIV id="Products_Grid"

DIV Products_Grid includes PRODUCTS datagrid which shows N records.

The DataGrid has 3 database fields:
1 - [Image01]
2 - [Title]
3 - [Price]

B) DIV id="Product_Detail"

In DIV Product_Detail I have:
1 - One ASP:Image - [Image01] database field by default. Full Size.
2 - Three ASP:ImageButtons - 3 thumbnail images using the fields:
[Image01], [Image02], [Image03]
3 - Four ASP:Labels - [Title], [Description], [Price],
Code:
4. My functions

A) Build_Grid()
Builds the PRODUCTS datagrid, binds it to data...

B) Change_Product()
When a record in datagrid is pressed it changes all the ASP:Image,
ASP:ImageButtons and ASP:Labels in DIV Product_Detail.

Like a Master-Detail in the same page.

C) Change_Photo()
When an ASP:ImageButton is pressed it gets its value and change the
URL of full size image displayed in ASP:Image in DIV Product_Detail.

This is where I have the problem!

Change_Photo() receives the field to use from the ASP:ImageButton.
I am using e.CommandName.

Q: But what record is being displayed? What is its productid?
A: It is the same as last record pressed in the datagrid.


So my implementation is:
 [QUOTE]
Change_Product() is called THEN: [/QUOTE]

Global variable CurrentProductID = ProductID of row pressed in datagrid.
Change Product_Detail Labels and Images values using this record value.
 [QUOTE]
Change_Photo() is called THEN[/QUOTE][/QUOTE]
[QUOTE]
Get new Thumbnail to show in ASP:Image (full size) from e.CommmandName
Get CurrentProductID
Change ASP:Image "Photo" using the 2 values.

Well, this might be confusing but it's nothing more then a master-detail
page in some page and in the detail part I have 3 thumbnails with a full
size feature for them.

Everything is working but when I click the image buttons it's like the
global variable CurrentProductID is not available. I have no idea why.

Thanks,
Miguel
















 [/QUOTE]
[QUOTE]
[/QUOTE][/QUOTE]
 
Ad

Advertisements

P

Patrice

Looks like point 1 in my previous message.

In ASP.NET the page object is created each time there is an HTTP request. If
you give a value to a public variable in the first request, the value will
not be there during the next request (as this is a new page object) unless
you restored it...

Patrice

--

Shapper said:
Got lost. :)

Well, I made this work using a Session Variable which holds the
productid value which is in use in the Detail Page.

The Session Value is reset only when Page_Load.

Does this allows to take any conclusion of what was happening when I was
using the global variable?

Cheers,
Miguel

Thanks Miguel,

I think it's getting a lot clearer.

I'm a little fuzzy on what the exact sequence of PostBacks and data changing
is. Something changes the value of CurrentProductId, I think. But what
persists that value across PostBacks? Remember that a Page class lasts for
the time it takes to process a Request. Afterwards, the class is destroyed,
and rebuilt with the next Request. That is why ViewState exists, to restore
the values of Controls after a PostBack. But I can't tell for sure whether
the value is set during one PostBack and read in another, or how you are
persisting this value across PostBacks.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

Hi,

Yes, I have to admit my explanation was confusing.

1. All the functions are in Class Catalogue.
2. Yes, class catalogue is derived from Inherits System.Web.UI.Page.
The class is in catalogue.aspx.vb. It has the code for catalogue.aspx.

3. In my aspx page (HTML code) I have 2 div's:

A) DIV id="Products_Grid"

DIV Products_Grid includes PRODUCTS datagrid which shows N records.

The DataGrid has 3 database fields:
1 - [Image01]
2 - [Title]
3 - [Price]

B) DIV id="Product_Detail"

In DIV Product_Detail I have:
1 - One ASP:Image - [Image01] database field by default. Full Size.
2 - Three ASP:ImageButtons - 3 thumbnail images using the fields:
[Image01], [Image02], [Image03]
3 - Four ASP:Labels - [Title], [Description], [Price],
Code:
4. My functions

A) Build_Grid()
Builds the PRODUCTS datagrid, binds it to data...

B) Change_Product()
When a record in datagrid is pressed it changes all the ASP:Image,
ASP:ImageButtons and ASP:Labels in DIV Product_Detail.

Like a Master-Detail in the same page.

C) Change_Photo()
When an ASP:ImageButton is pressed it gets its value and change the
URL of full size image displayed in ASP:Image in DIV Product_Detail.

This is where I have the problem!

Change_Photo() receives the field to use from the ASP:ImageButton.
I am using e.CommandName.

Q: But what record is being displayed? What is its productid?
A: It is the same as last record pressed in the datagrid.


So my implementation is:
[/QUOTE]
[QUOTE]
Change_Product() is called THEN:
[/QUOTE]
[QUOTE]
Global variable CurrentProductID = ProductID of row pressed in datagrid.
Change Product_Detail Labels and Images values using this record value.
[/QUOTE]
[QUOTE]
Change_Photo() is called THEN[/QUOTE]
[QUOTE]
Get new Thumbnail to show in ASP:Image (full size) from e.CommmandName
Get CurrentProductID
Change ASP:Image "Photo" using the 2 values.

Well, this might be confusing but it's nothing more then a master-detail
page in some page and in the detail part I have 3 thumbnails with a full
size feature for them.

Everything is working but when I click the image buttons it's like the
global variable CurrentProductID is not available. I have no idea why.

Thanks,
Miguel
[/QUOTE]
[QUOTE]
Hi Miguel,

I'm still confused. Sorry. First, what exactly do you mean by "region?"
In
Visual Studio.Net, a region is simply an organizational tool for use in
development, and has nothing to do with how an app works. In an ASP.Net
page, I have no idea what the word is supposed to represent.

Second, as I mentioned before, I saw some of your code, which defined a
class called "catalogue." Here is a reproduction of the code you posted
initially:
[/QUOTE]
[QUOTE]
Public Class catalogue
Public productid As String
Private Sub Page_Load
...
[/QUOTE]
[QUOTE]
Now, the Page_Load Sub leads me to believe that this class is somehow
supposed to be derived from System.Web.UI.Page, but there is no
"Inherits"
statement that indicates that it does. So, part of my confusion extends
from
that.

After the ellipse, you state:
[/QUOTE]
[QUOTE]
I have an image button where I call the following function:

Public Sub MyFunction(ByVal sender As Object, ByVal e As
CommandEventArgs)
[/QUOTE]
[QUOTE]
I'm not at all sure of what this class has to do with your Page, if
anything. The variable (field) "productid" is a public field of the
"catalogue" class. So, since I don't know what's going on with regards to
that class, I can't figure out what exactly is going wrong.

I don't know where you "have" this image button. If it is a member of the
"catalogue" class, what again does it have to do with an ASP.Net Page
class,
which you have talked about, but doesn't seem to appear anywhere in your
code?

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.
[/QUOTE]
[QUOTE]
Hello,

I will try to describe my problem.

In my page I have 2 distinct regions:

1. Datagrid which show a list of all products.

2. Region with 4 ASP Labels, 1 ASP Image and 3 ASP Image Buttons.
This will show the detailed information of a certain product.

So when I choose a datagrid record the function Product_Detail is
called
and the Labels, Image and Image Buttons are filled with the right
productid.

I mentioned I have 3 ImageButtons in region 2 of the page. Why?
These buttons show image thumbnails and when clicked call the function
Zoom_Image which will show the right image in the ASP:Image.

When Zoom_Image runs it needs to know which productid is current used
in
region 2. So everytime I click my datagrid I set the global variable
currentproductid=productid clicked.

This way I when I press one of the image buttons the zoom function
knows
which record is in use.

Does this make any sense?

Somewhere here the global variable currentproductid.

Maybe my approach is wrong.

Any idea?

Thanks,
Miguel
[/QUOTE]
[QUOTE]
I can see the e.CommandName value but not the productid.
[/QUOTE]
[QUOTE]
Where can you see it?

Also, you just might want to inherit System.Web.UI.Page in your class
if
you
expect any System.Web.UI.Page events (such as Page_Load) to fire.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.
[/QUOTE]
[QUOTE]
Hello,

I am declaring a variable in my aspx.vb code as follows:

Public Class catalogue
Public productid As String
Private Sub Page_Load
...

I have an image button where I call the following function:

Public Sub MyFunction(ByVal sender As Object, ByVal e As
CommandEventArgs)

Response.Write(productid)
Response.Write(e.CommandName)

End Sub

I can see the e.CommandName value but not the productid.

Is not productid global the way I declare it?

Thanks,
Miguel
[/QUOTE]
[QUOTE]
[/QUOTE][/QUOTE]
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top