?
Solved

ASP.net variables declared at top of web form

Posted on 2012-08-20
4
Medium Priority
?
522 Views
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?
0
Comment
Question by:Murray Brown
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38315119
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
    Response.Write(mintSomeID.ToString())
  End Sub
End Class

Open in new window

Alan
0
 
LVL 20

Accepted Solution

by:
informaniac earned 1000 total points
ID: 38315132
Globally declaring the variables will hold the values.
0
 
LVL 26

Assisted Solution

by:Alan Warren
Alan Warren earned 1000 total points
ID: 38315249
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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
    Else
      Label1.Text = mintSomeID.ToString()
    End If

  End Sub

  Protected Sub PostbackButton_Click(sender As Object, e As System.EventArgs)
    mintSomeID += 1
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:Button ID="PostbackButton" runat="server" Text="Postback" 
        onclick="PostbackButton_Click" />
      <br /><asp:Label ID="Label1" runat="server" Text=""></asp:Label>    
    </div>
    </form>
</body>
</html>

Open in new window

Alan
0
 

Author Closing Comment

by:Murray Brown
ID: 38315629
Thanks for the help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

840 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