I want to declare a integer variable in global scope but do not want to that its value become 0 every time the page is post-back

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Error : System.Web.UI.Page
{//should i declare here????
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Recommended Answers

All 4 Replies

>I want to declare a integer variable in global scope

You can store value with global scope.

protected void Page_Load(object sender, EventArgs e)
    {
        Application.Lock();
        if(Application["key1"]==null) {
             Application["key1"]=10;
        }
        
        int n=(int)Application["key1"];
       Application.Unlock();
     }

use the

if(!Page.IsPostback)
{

}

keep it static

yes you can declare there with static keyword.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.