locked
Wiring an event in asp.net getting called. But properties of pages set are not getting reflected. RRS feed

  • Question

  • User-1146789036 posted

    I have a 7  web pages and a class that declares some properties, event and some functions. The object of this class i.e. _ItemSetup is moved into all the pages and same copy is maintained on all the pages. Based on this object of ItemSetup class certain business decisions are taken and values are changed.

        public class ItemSetup 
        {
    
            /// <summary>
            /// Raises an event after FK_ITEM_TYPE property has changed.  
            /// PropertyChanged object returns value of FK_ITEM_TYPE before change and after change
            /// </summary>
            public event EventHandler<PropertyChanged> ItemTypeChanged;
    
    
            private long fK_ITEM_TYPE;
            /// <summary>
            /// Item type can be-Package, Subassembly, cash and many more.
            /// So each item type will have different behaviour based on which validation changes.
            /// "ItemTypeChanged" event is triggered when this property value is changed.
            /// </summary>
            public long FK_ITEM_TYPE 
            {
                get { return fK_ITEM_TYPE; }
                set 
                {
                    if (value == fK_ITEM_TYPE)
                        return;
                    fK_ITEM_TYPE = value;
    
                    PropertyChanged eOnChanged = new PropertyChanged(null, value);
                    if (ItemTypeChanged!= null)
                        ItemTypeChanged(this, eOnChanged);
                }
            }
         }
    
    **Code behind file for raising an event.**
    
        public partial class ItemMaster_Main : System.Web.UI.Page
        {
    
            public ItemSetup _ItemSetup { get; set; }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                ItemSetup.MenuBarEventAssociation(ItemMenu, SaveInputValues);
    
                if (!IsPostBack)
                {
                    BindDropDown();
    
                    if (Session["ItemSetup"] != null)
                    {
                        _ItemSetup = Session["ItemSetup"] as ItemSetup;
                        //Do something.
                    }
                    else
                    {
                        _ItemSetup = new ItemSetup(ItemSetup.OperationMode.Add);
                        _ItemSetup.ItemTypeChanged += new EventHandler<PropertyChanged>(_ItemSetup_ItemTypeChanged);
                        
                        Session["ItemSetup"] = _ItemSetup;
                    }
                }
                _ItemSetup = Session["ItemSetup"] as ItemSetup;
            }
    
            protected void cmbItemType_SelectedIndexChanged(object sender, EventArgs e)
            {
                _ItemSetup.FK_ITEM_TYPE = long.TryParse(cmbItemType.SelectedValue, out outLong) ? outLong : (long)L_ITEM_TYPE.SelectOne;
    
                // When I calls this as a functions. It do its work and sets the css.
                //_ItemSetup_ItemTypeChanged(null, null);
            }
    
            void _ItemSetup_ItemTypeChanged(object sender, PropertyChanged e)
            {
                //Hide or show button based on conditions
                btnNext.Visible = !(_ItemSetup.FK_ITEM_TYPE == (int)AARFID.DataLayer.L_ITEM_TYPE.SelectOne);
    
                //Hide all controls until Item Type is not selected.           
                if (_ItemSetup.FK_ITEM_TYPE == (long)L_ITEM_TYPE.SelectOne)
                    divControlBox.Attributes.Add("style", "display:none;");
                else
                    divControlBox.Attributes.Add("style", "display:block;");
            }
        }

    When the property FK_ITEM_TYPE is set at any point the event _ItemSetup_ItemTypeChanged gets raised and all the code written into the funtion gets executed. I expected that controls should have been hidden. But it did nothing to the page.


    I am not getting the reason why this event is not effecting the page.When I uncomments the line in function cmbItemType_SelectedIndexChanged

    i.e. 

    _ItemSetup_ItemTypeChanged(null,> null);
    the properties and css gets set and page behaves as expected.
    What could be the reason.

    Tuesday, May 24, 2011 6:42 AM

Answers

  • User863160722 posted

    You're only wiring up the event handler on the first request to the page. You need to add the event handler on every load.

    protected void Page_Load(object sender, EventArgs e)
    {
        ItemSetup.MenuBarEventAssociation(ItemMenu, SaveInputValues);
        
        if (!IsPostBack)
        {
            BindDropDown();
            
            if (Session["ItemSetup"] == null)
            {
                _ItemSetup = new ItemSetup(ItemSetup.OperationMode.Add);
                Session["ItemSetup"] = _ItemSetup;
            }
        }
        
        _ItemSetup = Session["ItemSetup"] as ItemSetup;
        if (_ItemSetup == null) 
        {
            throw new InvalidOperationException("Session expired?");
        }
        
        _ItemSetup.ItemTypeChanged += _ItemSetup_ItemTypeChanged;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 25, 2011 2:59 PM

All replies

  • User863160722 posted

    You're only wiring up the event handler on the first request to the page. You need to add the event handler on every load.

    protected void Page_Load(object sender, EventArgs e)
    {
        ItemSetup.MenuBarEventAssociation(ItemMenu, SaveInputValues);
        
        if (!IsPostBack)
        {
            BindDropDown();
            
            if (Session["ItemSetup"] == null)
            {
                _ItemSetup = new ItemSetup(ItemSetup.OperationMode.Add);
                Session["ItemSetup"] = _ItemSetup;
            }
        }
        
        _ItemSetup = Session["ItemSetup"] as ItemSetup;
        if (_ItemSetup == null) 
        {
            throw new InvalidOperationException("Session expired?");
        }
        
        _ItemSetup.ItemTypeChanged += _ItemSetup_ItemTypeChanged;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 25, 2011 2:59 PM
  • User-481631678 posted

    YOur Page_Load code should be like this:-

     

          protected void Page_Load(object sender, EventArgs e)
            {
                ItemSetup.MenuBarEventAssociation(ItemMenu, SaveInputValues);
    
                if (!IsPostBack)
                {
                    BindDropDown();
                }
    
                    if (Session["ItemSetup"] != null)
                    {
                        _ItemSetup = Session["ItemSetup"] as ItemSetup;
                        //Do something.
                    }
                    else
                    {
                        _ItemSetup = new ItemSetup(ItemSetup.OperationMode.Add);
                        _ItemSetup.ItemTypeChanged += new EventHandler<PropertyChanged>(_ItemSetup_ItemTypeChanged);
                        
                        Session["ItemSetup"] = _ItemSetup;
                    }
                _ItemSetup = Session["ItemSetup"] as ItemSetup;
            }
    

    Thursday, May 26, 2011 1:42 AM
  • User863160722 posted

    That would be even worse - you're only wiring up the event handler the first time the user requests a page in a single session!

    Thursday, May 26, 2011 7:37 AM