开发者

Dynamic Event Handler lost after postback

I have a asp.net page with a button, the event the button needs to perform on it's click event needs to vary, dependant on what options are selected further up the page. The button itself is included in the master page, and it's actions are affected by selections made in the child page.

To do this, I have a method in the master page, that is called by the child page, when a users presses a button, that passes the Eventhandler to be used by that button, that is then assigned to masterpage eventhandler for that button:

  public void assignEvent( EventHandler saveEvent)
    {
            this.SaveButtonEvent+= saveEvent
    }

Then, when the save button on the master page, it calls that eventhandler

protected void save_Click(object sender, EventArgs e)
    {
        if (this.SaveButtonEvent != null)
        {
            this.SaveButtonEvent(this, e);
        }
    }

The event handler is assigned ok in the开发者_StackOverflow中文版 first section of code, however because pressing the save button causes a postback, the SaveButtonEvent event handler is set back to being null, and so nothing happens.

How can I preserve the contents of SaveButtonEvent Event Handler during postback, or is there a better way to be doing this?

EDIT:

I can get this to work by saving the EventHandler to the session, but this doesn't seem like a great idea.


When the user presses the button in the child page, store a value (in the viewstate) indicating what event handler should be passed. Then, in the load event of the child page, if it is a postback, always check the stored value and assign the event handler accordingly by calling the master page method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜