1

I forgot to mention this asp.net 2.0.

The user control has a unique id and it is loaded in the PageLoad event. The user control is loaded into a panel and the panel is inside of a webpart. The dropdown has autopostback set to true.

EnableViewState = true on the dropdown. The ListItems are created in the dropdowns pre-render event method.

This is why I don't understand why it is not firing, the dropdown is the only thing that causes postback on this user control.

The event methods for the dropdown should occur since the user control is loaded in the page load method on postback again right?

OutOFTouch
  • 1,017
  • 3
  • 13
  • 30

2 Answers2

2

Make sure there is no OnLoad or PageLoad event that is rebinding the datasource of the dropdown list. Rebinding the data with a new set of data may cause the clickhandler to not ever get executed.

make sure you have if (!Page.IsPostBack) around dropdownlist.datasource = and dropdownlist.databind()

ronaldwidha
  • 1,345
  • 2
  • 12
  • 24
  • Nope the only place the list items are created is in the PreRender method for the dropdown. There is no databinding being used, I just create new list items in the PreRender method. – OutOFTouch Feb 21 '10 at 23:57
  • The key here is not to add new items or rebind at all on PostBack. – Bryan Feb 22 '10 at 00:00
  • I get that re adding the items would wipe out the events if done at the wrong point in page processing, I thought that the onSelectedChangedEvent for the dropdown would fire though since the items are added in the dropdowns pre-render but the dropdown is in a UserControl and the user control is loaded in Page Load would that make a difference? – OutOFTouch Feb 22 '10 at 03:11
  • PreRender normally is used for changing the content of a control. For example, parsing strings, string encoding, etc http://msdn.microsoft.com/en-us/library/ms178472.aspx Like Bryan said, the key here is to not rebind data on postback when you have viewstate turned on. What worries me though is "user control is loaded in Page Load". If you mean, dynamically load the user control from the code behind by using Control.Load, you have to create your own viewstate storage as by default custom user control doesn't get stored in the view state. you may not hit this edge case, mileage will vary. – ronaldwidha Feb 22 '10 at 04:31
  • Worries me too, because on initial page load the default view(default user control is loaded using Control.Load), than again in postback the current view is again loaded in Page load using Control.Load, but here is the kicker, there is a treeview in the page that changes the view when the selected node changes so the OnSelectedNodeChanged event loads a new user control using Control.Load. – OutOFTouch Feb 22 '10 at 17:38
  • Also if the user control is loaded in PageLoad IsPostBack will be true, I have tried a work around by creating a IsUserControlPostBack property and saving it to viewstate but it is not working for me since the prerender for the dropdown that checks this value is executed after it has been set to true in onLoad event. – OutOFTouch Feb 22 '10 at 19:02
  • Thanks, I moved the loading of the default views into the Page_init, and I make sure that my dropdown does not rebind or recreate it's items again and everything fires. – OutOFTouch Feb 23 '10 at 22:50
  • Yes, Control.Load does crazy things with the pagelifecycle, as depending to when the statement is made, the loaded control will try to catch up with the parent's event lifecycle. – ronaldwidha Feb 24 '10 at 14:50
  • hmm what if you load your user controls in page_init always which is before viewstate is loaded but in your control you postback like a refresh button in the user control and you need to get new data for a gridview or something, if you went and got it than Viewstate would wipe this out right? – OutOFTouch Feb 25 '10 at 02:41
1

I am not sure if this is your problem, but it is the most common.

Try with EnableViewState set to true for the DropDownList

If the ViewState is set to false, on post back the selected Index gets back to default which is normally the first Item. First item, if selected, does not cause SelectedIndexChange event to fire

Asad
  • 21,468
  • 17
  • 69
  • 94