Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 01, 2012 08:14 AM by Fadao1
Apr 17, 2009 02:48 PM|LINK
I am using Visual Studio 2008 to build a webpage with nested Master Pages. My hierarchy is thus: BGMaster.master ->FunctionMaster.master -> PowerDisplay.aspx.
selected = mnuMenu.SelectedItem.Text
the function automatically creates the error:
"Handles clause requires a WithEvents variable defined in the containing type or one of its base types".
I have discovered the following:
If I comment out the 'Handles mnuMenu.MenuItemClick' clause and add a declaration: 'Protected WithEvents mnuMenu as System.Web.UI.WebControls.Menu' at the top, the error goes away... but of course this means any attempt to access the Menu object inside
the function returns a Null pointer... because the function isn't accessing the real Menu object... but an empty variable I declared at the top of the form.
If I create the Menu object on the BGMaster, the same thing happens. If I drop it on the PowerDisplay, a regular aspx page, this does not occur.
If I attempt to declare the WithEvents handler anywhere else, I get an error that says it's already been declared.
What must I do to be able to handle events in my FunctionMaster.master.vb page for objects declared on my FunctionMaster.master page?
Apr 17, 2009 03:37 PM|LINK
This may happen if you are placing the menu control inside another control on the masterpage ,
Please remove the handles clause and instead , assing the event handler declaratively ,
<asp:Menu ID="mnuMenu" runat="server" OnMenuItemClick="mnuMenu_MenuItemClick">
Apr 17, 2009 04:04 PM|LINK
Thank you, but I should have mentioned I already tried that.
The end result is that, at runtime, the line:
causes a crash, because when it fires, the mnuMenu item is a null pointer.
Any other ideas appreciated, however.
Apr 17, 2009 05:20 PM|LINK
need to ask you something, why are you trying to find the menu control on FunctionMaster. If i am not mistaken the menu is already on the Function Master Page
I have dropped a Menu object (System.Web.UI.WebControls.Menu) onto my FunctionMaster.master, inside of a Content control. The object itself causes no problems, but when I create an Event Handler on the FunctionMaster.master.vb page for clicking on the menu:
and yes i did get the same error has you have mentioned about the protected with menuitems. But when i tried it with directly adding like
in ChildMaster Page, ( as Mr.Anas suggested) it worked fine. here take a look this
and code behind :
protected void MainMenu_MenuItemClick(object sender, MenuEventArgs e)
Label1.Text = MainMenu.SelectedItem.Text;
I had no problems using this in the content page. I do agree the above code is in C#, i hope you can convert it.
Hope this helps.
Apr 17, 2009 07:23 PM|LINK
The reason why I am trying to find the Menu object in the VB code is because I cannot access it directly, as your example does. If I remove the code to get at the mnuMenu object, it produces the following error:
"Name 'mnuMenu' is not declared."
For some reason, inside my vb code, I cannot access the mnuMenu object, even though I'm calling it from inside a function of the mnuMenu object, and I'm definitely in the vb page that corresponds to the aspx page.
I cannot imagine why your code does not produce the same error mine does. The language shouldn't make the difference.
Does this help clarify matters?
Apr 22, 2009 02:11 AM|LINK
Please check this thread, this thread show you how to access a controls which in the nested master page,
and please use this tool online to convert C# to VB if you are not familiar with C#,
Aug 01, 2012 08:14 AM|LINK
I had the same problen and I had to declare the button using WithEvents.