Asp – Raise OnMenuItemClick without ViewState

asp.netviewstate

I have a (derived) Menu control, that displays a rather large list of items from a custom data source. I need to disable ViewState on the menu to avoid the very annoying "Can't select a disabled or unselectable menu item" when some other control causes the current selection to change on a postback.

Unfortunately, when ViewState is disabled for the Menu, the postbacks generated by the menu aren't raising any events. If I enable ViewState, the OnMenuItemClick event is raised. If I disable ViewState, OnMenuItemClick is not raised. I'm perplexed.

I need to leave ViewState off for the menu, so how can I handle postbacks from the actual menu?

At this point I'm leaning towards using the Menu's Load event, parsing the __EVENTTARGET to see if it's the Menu, and going from there. This would technically process the postback event before it would normally but that's ok, I guess.

Any better ideas?

Best Answer

Yep, you can choose, either use viewstate to repopulate a bound control or you can databind it before events are fired (Page_Load is fine).

I wouldn't necessarily always bind it afresh in Page_PreRender though, if nothing has changed on this postback (changes happened somewhere else on the page) then there is no reason to bind it again.

Instead you might be able to bind only on certain events when you know it will have to change.

Related Topic