Eitan
Eitan

Reputation: 1

Web user control loses properties on postback

I created a usercontrol that has a public event named "DialogClosed".

This usercontrol also have a button. When the user clicks on the button, the background code fires the "DialogClosed" event.

The problem is that after the user clicked on the button, it caused to postback, which created a new instance of my control, and my control lost all his properties because the original instance no longer exist.

So when I fire the event, it throws an exception (because "DialogClosed" == null)

Upvotes: 0

Views: 2430

Answers (3)

Fred
Fred

Reputation: 1234

you need to re-bind the event handler after the page is post back. but the properties should not be lost if viewstate is enabled.

Upvotes: 0

Pete H.
Pete H.

Reputation: 1427

Maybe I'm missing something...but couldn't you just check against the IsPostback property of the page when you create the control, to ensure the control is not created on a postback?

Also, in what event are you creating the control?

Upvotes: 1

George
George

Reputation: 7954

Is ViewState enabled for the control?

If it is, the UserControl needs to be instantiated on the page's Init Event so the properties are persisted on a post back, unless you've simply declared it in the aspx markup.

Upvotes: 0

Related Questions