I am using Visual Studio 2008 to build a webpage with nested Master Pages. My hierarchy is thus: BGMaster.master ->FunctionMaster.master -> PowerDisplay.aspx.
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:
Public
Sub mnuMenu_MenuItemClick(ByVal
sender As
Object,
ByVal e
As MenuEventArgs)
Handles mnuMenu.MenuItemClick
Dim selected
As
String =
""
Dim mnuMenu
As Menu =
DirectCast(Me.FindControl("mnuMenu"),
Menu)
Dim selectedItem
As MenuItem = mnuMenu.SelectedItem
selected = mnuMenu.SelectedItem.Text
MsgBox(selected)
End
Sub
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?
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
JDMartin
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
OnMenuItemClick
="MainMenu_MenuItemClick"
in ChildMaster Page, ( as Mr.Anas suggested) it worked fine. here take a look this
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.
JDMartin
0 Points
6 Posts
"Handles Clause Requires A WithEvents..."
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.
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:Public
Sub mnuMenu_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs) Handles mnuMenu.MenuItemClick Dim selected As String = "" Dim mnuMenu As Menu = DirectCast(Me.FindControl("mnuMenu"), Menu) Dim selectedItem As MenuItem = mnuMenu.SelectedItemselected = mnuMenu.SelectedItem.Text
MsgBox(selected)
End Subthe 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?
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: "Handles Clause Requires A WithEvents..."
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">
JDMartin
0 Points
6 Posts
Re: "Handles Clause Requires A WithEvents..."
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:
Dim
selectedItem As MenuItem = mnuMenu.SelectedItemcauses a crash, because when it fires, the mnuMenu item is a null pointer.
Any other ideas appreciated, however.
novicehere
Contributor
4654 Points
854 Posts
Re: "Handles Clause Requires A WithEvents..."
Apr 17, 2009 05:20 PM|LINK
Hello There,
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
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
OnMenuItemClick
="MainMenu_MenuItemClick"in ChildMaster Page, ( as Mr.Anas suggested) it worked fine. here take a look this
<%
@ Master Language="C#" MasterPageFile="~/MasterPages/parent.master" AutoEventWireup="true" CodeFile="Child2.master.cs" Inherits="NestedMasterPages_Child2" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></
asp:Content><
asp:Content ID="Content2" ContentPlaceHolderID="cpParent" Runat="Server"> <div> <asp:Menu ID="MainMenu" runat="server" StaticDisplayLevels="2" Orientation="Horizontal" StaticEnableDefaultPopOutImage="false" DynamicEnableDefaultPopOutImage="false" OnMenuItemClick="MainMenu_MenuItemClick" > <Items> <asp:MenuItem Text="Home" Value="Home" > <asp:MenuItem Text="MenuItem1" Value="mt1"></asp:MenuItem> <asp:MenuItem Text="MenuItem2" Value="mt2"></asp:MenuItem> </asp:MenuItem> </Items> </asp:Menu>
<asp:Label ID="Label1" runat="server"></asp:Label> </div></
asp:Content>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.
Thanks
Please Remember to Mark as Answer for the post(s) that help you.....so it can help others......Thanks
JDMartin
0 Points
6 Posts
Re: "Handles Clause Requires A WithEvents..."
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?
Hong-Gang Ch...
All-Star
74696 Points
6768 Posts
Re: "Handles Clause Requires A WithEvents..."
Apr 22, 2009 02:11 AM|LINK
Got it.
Please check this thread, this thread show you how to access a controls which in the nested master page,
http://forums.asp.net/t/1270231.aspx
and please use this tool online to convert C# to VB if you are not familiar with C#,
http://www.developerfusion.com/tools/convert/csharp-to-vb/
If you have any feedback about my replies,please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
Fadao1
Member
14 Points
8 Posts
Re: "Handles Clause Requires A WithEvents..."
Aug 01, 2012 08:14 AM|LINK
I
I had the same problen and I had to declare the button using WithEvents.
Protected WithEvents LinkButtonSearch As LinkButton
Protected Sub LinkButtonSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButtonSearch.Click
End Sub
Angola