K
kaczmar2
I have a custom web control that is called from an aspx page. The
custom control dynamically renders out buttons via a public method
(AddButton). One of the attributes of the method is a delegate, so on
the button click event I can invoke a method at the calling page
level. Everything works great if the control is written in C#, but it
needs to be written in VB.NET.
In C#, in the control I can attach an event like so:
NewButton.Click += new EventHandler(Method);
where "Method" is the name of the delegate parameter in the AddButton
function.
In VB.NET, I try to attach an event like so:
AddHandler NewButton.Click, AddressOf Method
This throws the error: 'AddressOf' operand must be the name of a
method (without parentheses).
If I pass AddressOf a method declared at the page level, it works, but
I need to pass it the delegate param that is passed in to the
function. How can I do this in VB.NET? code is below. Thanks for
your help!
Here is the code on the aspx page:
// Button_Click is a delegate, and is defined in the calling page:
MyCustomControl1.AddButton("btnTest", "A Test", Button_Click);
USER CONTROL CODE : C# -- this works...
public delegate void MyDelegate(object sender, EventArgs e);
public void AddButton(string Id, string ButtonText, MyDelegate Method)
{
MyCoolButton NewButton = new Button();
NewButton.ID = Id;
NewButton.Text = ButtonText;
//NewButton.Click += new EventHandler(EventHandler);
NewButton.Click += new EventHandler(Method);
Buttons.Add(NewButton);
}
USER CONTROL CODE : VB.NET - this does not work...
Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)
Public Sub AddButton(ByVal Id As String, ByVal ButtonText As String,
ByVal Method As MyDelegate)
Dim NewButton As New Button
NewButton.ID = Id
NewButton.Text = ButtonText
AddHandler NewButton.Click, AddressOf Method ' *** THIS IS THE LINE
WITH THE ERROR
Buttons.Add(NewButton)
End Sub
custom control dynamically renders out buttons via a public method
(AddButton). One of the attributes of the method is a delegate, so on
the button click event I can invoke a method at the calling page
level. Everything works great if the control is written in C#, but it
needs to be written in VB.NET.
In C#, in the control I can attach an event like so:
NewButton.Click += new EventHandler(Method);
where "Method" is the name of the delegate parameter in the AddButton
function.
In VB.NET, I try to attach an event like so:
AddHandler NewButton.Click, AddressOf Method
This throws the error: 'AddressOf' operand must be the name of a
method (without parentheses).
If I pass AddressOf a method declared at the page level, it works, but
I need to pass it the delegate param that is passed in to the
function. How can I do this in VB.NET? code is below. Thanks for
your help!
Here is the code on the aspx page:
// Button_Click is a delegate, and is defined in the calling page:
MyCustomControl1.AddButton("btnTest", "A Test", Button_Click);
USER CONTROL CODE : C# -- this works...
public delegate void MyDelegate(object sender, EventArgs e);
public void AddButton(string Id, string ButtonText, MyDelegate Method)
{
MyCoolButton NewButton = new Button();
NewButton.ID = Id;
NewButton.Text = ButtonText;
//NewButton.Click += new EventHandler(EventHandler);
NewButton.Click += new EventHandler(Method);
Buttons.Add(NewButton);
}
USER CONTROL CODE : VB.NET - this does not work...
Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)
Public Sub AddButton(ByVal Id As String, ByVal ButtonText As String,
ByVal Method As MyDelegate)
Dim NewButton As New Button
NewButton.ID = Id
NewButton.Text = ButtonText
AddHandler NewButton.Click, AddressOf Method ' *** THIS IS THE LINE
WITH THE ERROR
Buttons.Add(NewButton)
End Sub