locked
Global variable returns null after click event RRS feed

  • Question

  • User1575078971 posted

    i have to store click status as "clicked" after use it as in if block in edit row but the value setting null 

    public string btnStatus;
    
    private void bindSearchResult()
            {
     btnStatus = "clicked";
    lblMessage.Text = btnStatus; // lable is displaying fine 
    //code
        }
    if (e.CommandName == "EditRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                    gvMainCategory.EditIndex = rowIndex;
                    if (btnStatus == "clicked") // if btnstatus is null due to this it goes to else
                    {
                        bindSearchResult();
                    }
                    else
                    {
                        bindMC();
                    }

    Sunday, January 17, 2016 6:27 AM

Answers

  • User-986267747 posted

    Hi harshamrita,

    harshamrita

    i have to store click status as "clicked" after use it as in if block in edit row but the value setting null 

    As markfitzme say, the variable called btnStatus not a global variable, It's just a class field. In ASP.NET, a static fields lifetime is for the app domain (note that this differs a bit for generic types), so Static Variables are global. You could refer to the static variable instead of the field and use it to storage the data.

      public static string btnStatus = string.Empty;

    Besides, I'd recommend using the server Session to storage the data.

    Session["btnStatus"]="clicked",
    
    if(Session["btnStatus"] == null)
    {
       ...
    }
    else
    {
       
    }

    https://msdn.microsoft.com/en-us/library/ms178581.aspx

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 18, 2016 6:50 AM

All replies

  • User465171450 posted

    First, that's not a global variable. It's a class field that is accessible to all the items within the class.

    The problem here is lifetime of that variable. That will be destroyed just as soon as the page is done processing. Next time you attempt to do something with it, it's back to null value. You have to store it with some persistent mechanism. 

    Probably the easiest way to do this is to create a property that is backed by ViewState. This is easily done like so:

    public string btnStatus
    {
        get{
          if(ViewState["btnStatus"] != null)
          {
              return ViewState["btnStatus"].ToString();
          }
          else
          {
              return string.Empty;
          }
        }
        set {ViewState["btnStatus"] = value;}
    }

    This serializes the value into the viewstate so it's accessible across multiple postbacks on that page. 

    Sunday, January 17, 2016 5:45 PM
  • User-986267747 posted

    Hi harshamrita,

    harshamrita

    i have to store click status as "clicked" after use it as in if block in edit row but the value setting null 

    As markfitzme say, the variable called btnStatus not a global variable, It's just a class field. In ASP.NET, a static fields lifetime is for the app domain (note that this differs a bit for generic types), so Static Variables are global. You could refer to the static variable instead of the field and use it to storage the data.

      public static string btnStatus = string.Empty;

    Besides, I'd recommend using the server Session to storage the data.

    Session["btnStatus"]="clicked",
    
    if(Session["btnStatus"] == null)
    {
       ...
    }
    else
    {
       
    }

    https://msdn.microsoft.com/en-us/library/ms178581.aspx

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 18, 2016 6:50 AM
  • User1575078971 posted

    First, that's not a global variable. It's a class field that is accessible to all the items within the class.

    The problem here is lifetime of that variable. That will be destroyed just as soon as the page is done processing. Next time you attempt to do something with it, it's back to null value. You have to store it with some persistent mechanism. 

    Probably the easiest way to do this is to create a property that is backed by ViewState. This is easily done like so:

    public string btnStatus
    {
        get{
          if(ViewState["btnStatus"] != null)
          {
              return ViewState["btnStatus"].ToString();
          }
          else
          {
              return string.Empty;
          }
        }
        set {ViewState["btnStatus"] = value;}
    }

    This serializes the value into the viewstate so it's accessible across multiple postbacks on that page. 

    I not able to achive the goal please check my whole page code for clear view

    thanks  for your reply

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BussinessObject;
    using Bussinesslogic;
    
    namespace shoppingWeb
    {
        public partial class ManageCategory : System.Web.UI.Page
        {
            //variable for switch
            public string onoffswitch;
            public string btnStatus
            {
                get
                {
                    if (ViewState["btnStatus"] != null)
                    {
                        return ViewState["btnStatus"].ToString();
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                set { ViewState["btnStatus"] = value; }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindMC();
                }
            }
    
            //DataBinding method
            private void bindMC()
            {
                int counter = 1;
                counter = counter + 1;
    
                BL_MainCategory objBL_getMC = new BL_MainCategory();
                gvMainCategory.DataSource = objBL_getMC.LoadCategory();
                gvMainCategory.DataBind();
            }
            //Insert button 
            protected void btnInsert_Click(object sender, EventArgs e)
            {
                if (IsValid)
                {
                    BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                    
                    objBO_mainCategory.MCName = txtMCName.Text;
                    objBO_mainCategory.Description = txtDescription.Text;
                    
                    //checkbox code- start-//
                    onoffswitch = string.IsNullOrEmpty(Request["onoffswitch"]) ? string.Empty : "checked";
                    objBO_mainCategory.MCStatus = !string.IsNullOrEmpty(Request["onoffswitch"]);
                    //checkbox code- end-//
                    BL_MainCategory objInsertMC = new BL_MainCategory();//errot
                    int returncode = Convert.ToInt32(objInsertMC.BL_insertMCmethod(objBO_mainCategory));
                    if (returncode == 1)
                    {   
                        lblMessage.Text = "Data is inserted successfully";
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                        bindMC();
                    }
                    else
                    {
                        lblMessage.Text = "Main category already exist";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
    
            protected void btnSearchMC_Click(object sender, EventArgs e)
            {
                bindSearchResult();
            }
    
            private void bindSearchResult()
            {
                //HERE IS THE VIEWSTATE
                ViewState["btnStatus"] = "clicked";
                
                
                BL_MainCategory objBL_MainCategory = new BL_MainCategory();
                BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                objBO_mainCategory.MCName = txtSearchMC.Text;
                gvMainCategory.DataSource = objBL_MainCategory.BL_loadFindData(objBO_mainCategory);
                gvMainCategory.DataBind();
               
            }
            //edit update delete commands
            protected void gvMainCategory_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                    gvMainCategory.EditIndex = rowIndex;
                    // HERE IS THE IF BLOCK FOR VIEWSATE
                    if (ViewState["btnStatus"] != null)
                    {
                        bindSearchResult();
                    }
                    else
                    {
                        bindMC();
                    }
                }
               else if (e.CommandName == "DeleteRow")
                {
                    BL_MainCategory objBL_getMC = new BL_MainCategory();
                    BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                    objBO_mainCategory.MCId = Convert.ToInt32(e.CommandArgument);
                    int resut = Convert.ToInt32(objBL_getMC.BL_DeleteMCmethod(objBO_mainCategory));
                    bindMC();
                }
               else if (e.CommandName == "CancelUpdate")
                {
                    gvMainCategory.EditIndex = -1;
                    bindMC();
                }
                else if (e.CommandName == "UpdateRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                    BL_MainCategory objBL_MainCategory= new BL_MainCategory();
                    BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                    objBO_mainCategory.MCId = Convert.ToInt32(e.CommandArgument);
                    objBO_mainCategory.MCName = ((TextBox)gvMainCategory.Rows[rowIndex].FindControl("txtEditMCName")).Text;
                    objBO_mainCategory.Description = ((TextBox)gvMainCategory.Rows[rowIndex].FindControl("txtEditMCDescription")).Text;
                    objBO_mainCategory.MCStatus= ((CheckBox)gvMainCategory.Rows[rowIndex].FindControl("ckStatus")).Checked;
                    int resut = Convert.ToInt32(objBL_MainCategory.BL_UpdateMCmethod(objBO_mainCategory));
                    
                    gvMainCategory.EditIndex = -1;
                    bindMC();
                }
            }
    
            protected void txtSearchMC_TextChanged(object sender, EventArgs e)
            {
                bindSearchResult();
            }
        }
    }

    Tuesday, January 19, 2016 12:30 PM
  • User-986267747 posted

    Hi harshamrita,

    public string btnStatus { get { if (ViewState["btnStatus"] != null) { return ViewState["btnStatus"].ToString(); } else { return string.Empty; } } set { ViewState["btnStatus"] = value; } }

    You could remove the variable called btnStatus and use the ViewState["btnStatus"] directly.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BussinessObject;
    using Bussinesslogic;
    
    namespace shoppingWeb
    {
        public partial class ManageCategory : System.Web.UI.Page
        {
            //variable for switch
            public string onoffswitch;
         
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindMC();
                }
            }
    
            //DataBinding method
            private void bindMC()
            {
                int counter = 1;
                counter = counter + 1;
    
                BL_MainCategory objBL_getMC = new BL_MainCategory();
                gvMainCategory.DataSource = objBL_getMC.LoadCategory();
                gvMainCategory.DataBind();
            }
            //Insert button 
            protected void btnInsert_Click(object sender, EventArgs e)
            {
                if (IsValid)
                {
                    BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                    
                    objBO_mainCategory.MCName = txtMCName.Text;
                    objBO_mainCategory.Description = txtDescription.Text;
                    
                    //checkbox code- start-//
                    onoffswitch = string.IsNullOrEmpty(Request["onoffswitch"]) ? string.Empty : "checked";
                    objBO_mainCategory.MCStatus = !string.IsNullOrEmpty(Request["onoffswitch"]);
                    //checkbox code- end-//
                    BL_MainCategory objInsertMC = new BL_MainCategory();//errot
                    int returncode = Convert.ToInt32(objInsertMC.BL_insertMCmethod(objBO_mainCategory));
                    if (returncode == 1)
                    {   
                        lblMessage.Text = "Data is inserted successfully";
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                        bindMC();
                    }
                    else
                    {
                        lblMessage.Text = "Main category already exist";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
    
            protected void btnSearchMC_Click(object sender, EventArgs e)
            {
                bindSearchResult();
            }
    
            private void bindSearchResult()
            {
                //assign the ViewState["btnStatus"] with "clicked"
                ViewState["btnStatus"] = "clicked";
                
                
                BL_MainCategory objBL_MainCategory = new BL_MainCategory();
                BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                objBO_mainCategory.MCName = txtSearchMC.Text;
                gvMainCategory.DataSource = objBL_MainCategory.BL_loadFindData(objBO_mainCategory);
                gvMainCategory.DataBind();
               
            }
            //edit update delete commands
            protected void gvMainCategory_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                    gvMainCategory.EditIndex = rowIndex;
                    // HERE IS THE IF BLOCK FOR VIEWSATE
                    if (ViewState["btnStatus"] != null)
                    {
                        bindSearchResult();
                    }
                    else
                    {
                        bindMC();
                    }
                }
               else if (e.CommandName == "DeleteRow")
                {
                    BL_MainCategory objBL_getMC = new BL_MainCategory();
                    BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                    objBO_mainCategory.MCId = Convert.ToInt32(e.CommandArgument);
                    int resut = Convert.ToInt32(objBL_getMC.BL_DeleteMCmethod(objBO_mainCategory));
                    bindMC();
                }
               else if (e.CommandName == "CancelUpdate")
                {
                    gvMainCategory.EditIndex = -1;
                    bindMC();
                }
                else if (e.CommandName == "UpdateRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                    BL_MainCategory objBL_MainCategory= new BL_MainCategory();
                    BO_mainCategory objBO_mainCategory = new BO_mainCategory();
                    objBO_mainCategory.MCId = Convert.ToInt32(e.CommandArgument);
                    objBO_mainCategory.MCName = ((TextBox)gvMainCategory.Rows[rowIndex].FindControl("txtEditMCName")).Text;
                    objBO_mainCategory.Description = ((TextBox)gvMainCategory.Rows[rowIndex].FindControl("txtEditMCDescription")).Text;
                    objBO_mainCategory.MCStatus= ((CheckBox)gvMainCategory.Rows[rowIndex].FindControl("ckStatus")).Checked;
                    int resut = Convert.ToInt32(objBL_MainCategory.BL_UpdateMCmethod(objBO_mainCategory));
                    
                    gvMainCategory.EditIndex = -1;
                    bindMC();
                }
            }
    
            protected void txtSearchMC_TextChanged(object sender, EventArgs e)
            {
                bindSearchResult();
            }
        }
    }

    Besides, i suggest that you need to learn what is viewstate and how it works in asp.net.

    http://www.c-sharpcorner.com/UploadFile/225740/what-is-view-state-and-how-it-works-in-Asp-Net53/

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    Wednesday, January 20, 2016 5:19 AM