Solved variables declared at top of web form

Posted on 2012-08-20
Last Modified: 2012-08-21
Hi. I have declared two variables at the top of my web form

    Dim xSQLAdapter_Periods As SqlDataAdapter
    Dim xSQLTable_Periods As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

I need to use these variables in the code behind the webform several times while the page is running but am worried about whether they will hold their values. I have noticed that variables seem to lose their values after a post back. Should they hold or should I use a different approach?
Question by:murbro
    LVL 26

    Expert Comment

    by:Alan Warren
    Hi murbro,
    Partial Class Default4
      Inherits System.Web.UI.Page
      Private mintSomeID As Int32 = 0
      Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      End Sub
    End Class

    Open in new window

    LVL 20

    Accepted Solution

    Globally declaring the variables will hold the values.
    LVL 26

    Assisted Solution

    by:Alan Warren
    hi murbro,
    apologies, there was an error in my previous post.
    The following won't be available to other pages, but will be available to other script blocks within the same class or partial class; and definitely survives postbacks.
    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <script runat="server">
      Protected Shared mintSomeID As Int32
      Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
          mintSomeID = 0
          Label1.Text = mintSomeID.ToString()
        End If
      End Sub
      Protected Sub PostbackButton_Click(sender As Object, e As System.EventArgs)
        mintSomeID += 1
      End Sub
    <html xmlns="">
    <head runat="server">
      <form id="form1" runat="server">
          <asp:Button ID="PostbackButton" runat="server" Text="Postback" 
            onclick="PostbackButton_Click" />
          <br /><asp:Label ID="Label1" runat="server" Text=""></asp:Label>    

    Open in new window


    Author Closing Comment

    Thanks for the help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now