简体   繁体   中英

Button is not getting the ViewState Value and no error message shown

i have been trying to use the ViewState object to store the counter for clicking the ImageButton.For instance if ImageButton1 is click it will store the counter == 1(or increment) in it if another button is clicked the counter will become null.I tried clicking the imagebutton , the counter becomes 1 however when i try to retrieve from the submit button via the if else statement it retrieves nothing and the button cannot work.In addition no error message has been shown.I am developing something like a seat selector for my project.Any help will be greatly appreciated!.Below are the codes

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {


        if (ImageButton1.ImageUrl != "~/Images/bed-occupied.png")
        {
            ImageButton1.ImageUrl = "~/Images/bed-occupied.png";

            if (ViewState["Counter"] == null)
            {
                counterBed1 = 1;
                TextBoxClass.Text = counterBed1.ToString();
            }
            else
            {
                counterBed1 = (int)ViewState["Counter"] + 1;
            }

             }
        else
        {
            ImageButton1.ImageUrl = "~/Images/bed-unoccupied.png";
            ViewState["Counter"] = null;

        }

    }


 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ViewState["Counter"] = counterBed1;
       // if(ViewState["Counter"] != null)
        if(counterBed1 ==1)
        {



            Panel_ErrorMsg.Visible = true;
            lb_ErrorMsg.Text = "Patient Successfully admitted to hospital";

        }




        }

You are incrementing the value as well as setting a local variable but please note you are never storing the value back in ViewState object.

int counterBed1 = 0;
if (ImageButton1.ImageUrl != "~/Images/bed-occupied.png")
 {
      ImageButton1.ImageUrl = "~/Images/bed-occupied.png";
      if (ViewState["Counter"] == null)
      {
          counterBed1 = 1;
          TextBoxClass.Text = counterBed1.ToString();
          ViewState["Counter"] = counterBed1; // Add This
       }
       else
       {
           counterBed1 = (int)ViewState["Counter"] + 1;
           ViewState["Counter"] = counterBed1; //Add This
       }
  }
  else
  {
      ImageButton1.ImageUrl = "~/Images/bed-unoccupied.png";
      ViewState["Counter"] = null;
  }

Also, don't use class variable as it will be re-initialized after every new request, use local variable instead like this in Submit Button handler:-

protected void btnSubmit_Click(object sender, EventArgs e)
 {
     int counterBed1 = Convert.ToInt32(ViewState["Counter"]);
     // if(ViewState["Counter"] != null)
     if(counterBed1 ==1)
     {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM