views:

454

answers:

3

Hi,

i created a usercontrol that have public event named "DialogClosed".

this usercontrol also have a button.

i programmed it that 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)

Thanks, Eitan H.s.

+1  A: 

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?

Gus
A: 

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.

George
should i enable viewstate on the usercontrol itself or in the using pages?
Eitan
You stated...."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..."So it depends on what "MyControl" is. If the property in question belongs to the UserControl then that requires the UserControl to have viewstate enabled. If it is a custom property on the UserControl make sure this property updates viewstate accordingly.http://stackoverflow.com/questions/1673975/viewstate-as-attribute
George
A: 

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.

Estelle