开发者

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

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, eOnChange开发者_如何转开发d);
        }
    }
 }

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 function 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.

EDIT

if i associates event handler ourside IsPostBack then page gets effected as desired but events keeps adding on.

(_ItemSetup.ItemTypeChanged as EventHandler<PropertyChanged>).GetInvocationList()
{System.Delegate[17]}
    [0]: {Method = {Void ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [1]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [2]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [3]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [4]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [5]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [6]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [7]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [8]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [9]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [10]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [11]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [12]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [13]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [14]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [15]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}
    [16]: {Method = {Void _ItemSetup_ItemTypeChanged(System.Object, AARFID.DataLayer.PropertyChanged)}}


With every postback the _ItemSetup instance is losing the correct wiring of its eventhandlers so you have to add them with each page load and remove them on page unload. In short version:

protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["ItemSetup"] == null)
        {
            _ItemSetup = new ItemSetup();
            Session["ItemSetup"] = _ItemSetup;
        }

        _ItemSetup = Session["ItemSetup"] as ItemSetup;
        _ItemSetup.ItemTypeChanged += _ItemSetup_ItemTypeChanged;            
    }

protected void Page_Unload(object sender, EventArgs e)
    {
        _ItemSetup.ItemTypeChanged -= _ItemSetup_ItemTypeChanged;
    }


The page load method gets fired before it is handling the event. ASP page life cycle is fairly complicated to understand but you should let your event handlers do the work, not the page load method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜