سؤال

I have a user control with a DropDownList with AutoPostBack = true, also I have an aspx page to display this control. I re-create user control in OnInit method of the page

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    var list = (List<Control>)Session[Controls];
    if (list != null)
    {
        foreach (var control in list)
        {
          var uc = (Control)LoadControl(ControlPath);
          uc.SetDropDownState(control.state);
          PlaceHolderQuestion.Controls.Add(uc);
         }
    }
}

So while re-creating I am restoring DropDown selection, it works for the first time, but when I change selection again, OnSelectedIndexChanged event does not fire and it is obvious because I first restore DropDown selection in OnInit and so no OnSelectedIndexChanged event, cause nothing was changed, can you suggest some workaround?

UPDATE

var uc = (Control)LoadControl(ControlPath); is required to be keep user controls events

هل كانت مفيدة؟

المحلول

After a deep research, I figured out that the issue was in the SelectedIndexChanged event logic. I am using "Indirect Subscription" approach to handle it within my Page logic, for more info please take a look Indirect Subscription Approach

Originally I was reloading controls inside the handler but it was not required since I am doing it in the Page_Load method as well, so all I need to do is to update control inside my PlaceHolder controls list. I hope it is clear, if not, please ask, and I will provide more description.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top