Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Hello!!!

I have declared a variable under a class of my VB.NET 

<pre lang="vb">
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Private flg_edit As Integer

Whenever the webpage is refreshed this variable (flg_edit) is reset to 0 even if I had set it to 1 in the subsiquent code.

I want to use this variable throughout the webpage.
How do I do that?

Thanks!!!

Ernest.
Posted

What you have to understand is that each time you refresh the page, a new instance of that page is instantiated. Now, you may be wondering how items that have been changed in the page are set to their changed value.

To understand this (in classic ASP.NET), it's important to be aware of two things; the first is that ViewState is used to store changed values, and the second thing is to be aware that these values are used to repopulate the relevant sources (this is a high-faluting way of saying that you don't need to rebind most datasources because the bound data has been written to the ViewState for you automatically).

So, what does this really mean for you? Well, this means that you can store the value you want in the ViewState, and repopulate it when the ViewState is repopulating your page; or you could use the Session context to store the value, but considering that it's only relevant on this one page, that would be overkill.

Basically, you could simply do this in your code:
VB
If ViewState["EditNum"] Is Nothing Then
  flg_edit = 1
Else
  flg_edit = CType(ViewState["EditName"], Int32)
End If
And you save your ViewState like this:
VB
ViewState["EditNum"] = flg_edit
 
Share this answer
 
Comments
ernestmachado 27-Apr-12 1:30am    
Thank you Pete for your idea...
I need to know more about that...
I found a alternate solution as below ...
Hello!!!

I found a solution to my above difficulty as follows -

Just make the varible declared as shared so that it can be used throughout the current page without it been reset.

Doing this the refresh will not create a new instance of the variable.
VB
Dim Shared flg_edit As Integer
 
Share this answer
 
Check Solution 2 specifically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900