Setting SelectedValue of ASP.NET menu programatically
I have a MultiView and a Menu in my ASP.NET page - each menu item has a Value property which corresponds to the ViewIndex of the tab to show.
I will sometimes need to set the active view programatically, which works fine for the MultiView, but setting the Selected property of the Menu control is a bit more difficult. I could loop through each item til the value matches the view index I want to show, but th开发者_StackOverflow中文版is seems a bit hacky.
Any ideas?
I recommend using the MultiView's OnActiveViewChanged event to select the menu item.
protected void myMultiView_ActiveViewChanged(object sender, EventArgs e)
{
int index = ((MultiView)sender).ActiveViewIndex;
myMenu.FindItem(index.ToString()).Selected = true;
}
This way, whenever the view is changed (via SetActiveView(), or otherwise), the menu selection will remain synced. Note that you may also need to set the active view on the OnMenuItemClick
event of the menu.
精彩评论