0

I have a some the query on the .Net web form application.i have very simple web application which have a one button and label. i am doing some operation on the click event of the button.it's all fine,getting all expected output.but here one problem i am getting is the web page refresh.When i was refreshing the web browser program was starting.so to prevent it i used session method so program not starting after the refresh button but getting Object reference not set to an instance of an object error.

Please have a look on the code side. i am trying to avoid the error but it's behaving the same. when i click the refresh button on the browser i am getting error [NullReferenceException: Object reference not set to an instance of an object.]. this error is generating from the comparison of the both the state in button click function.

might be it's taking time to update the ViewState["CheckRefresh"] state. please suggest some solution for this.or is there any other method stop auto-start program on the web browser refresh.

        protected void Page_Load(object sender, EventArgs e)
        {

              localDirPath = Server.MapPath("~/");
              log.setLogPath(localDirPath);

              localStorePath = localDirPath + "/download/";

              if (!IsPostBack)
              {
                    Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
              }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
              Label1.Text = "";
              UpdateTimer.Interval = 100;

              if (ViewState["CheckRefresh"] != null && !ViewState["CheckRefresh"].Equals("-1"))
              {
                    if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
                    {

                          Session.Remove("JobManager");

                          //start the porcess if button click
                          Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());

                          UpdateTimer.Enabled = true;

                          Label1.Enabled = true;

                          isbuttonpressed = true;
                    }
                    else
                    {

                          //disable the process if browser the refresh

                          UpdateTimer.Enabled = false;
                          Label1.Enabled = false;

                    }
              }


        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
              ViewState["CheckRefresh"] = Session["CheckRefresh"];
        }
  • It's not really clear what you mean by *"web browser program was starting"* or *"stop auto-start program on the web browser refresh"*, but the exception itself is well documented and pretty much always means the same thing... The code is trying to de-reference an object that doesn't exist. Aside from that it's not really clear what all of these "CheckRefresh" values and checks are trying to do. – David Aug 16 '21 at 15:53
  • @David web browser program starting means if i pressed refresh button on the web-browse like crome or internet explore, the event is generating automatic physical click on the button. i found the "CheckRefresh" from the [link] http://csharpdotnetfreak.blogspot.com/2008/12/detect-browser-refresh-to-avoid-events.html – Ketan Vadodariya Aug 16 '21 at 16:20

0 Answers0