How to a call a function from a user control's parent page ?

  • Thread starter Thread starter grist2mill
  • Start date Start date
G

grist2mill

I want to create a standard tool bar that appears on all pages that is
a control. The toolbar has a button 'New'. What I wolud like when the
user clicks on 'New' depends on the page they are on. I would like to
do this by defining a NewFunc() that is different in each
(code-behind) page which is called by the standard 'New' button in the
toolbar.

How can I get this to work? So far I have the following (which doesnt
work). When the user clicks 'New', I would like it to call NewFunc()
as defined on the current page that is presenting the user control.

Private Sub lbtNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtSearch.Click
lblMessage.Text = MyClass.Page.NewFunc()
'lblMessage.Text = MyClass.Parent.Page.NewFunc()
'lblMessage.Text = MyBase.Page.NewFunc()

End Sub

(in the above example, NewFunc simply returns a string to be displayed
on a label control on the toolbar)

Any suggestions appreciated.
regards
John
 
John,
Not sure I understand, so let me repeat your problem so that you can
confirm.

-You have a user control (ascx) that'll be placed on each page
-The user control has a new button
-When the new button is clicked, you want to call a function from the page
(aspx)
-Obviously page could be one of any aspx files which has the user control on
it

If this is your problem, there are 2 good solutions (and one not so good
that I won't go into, but we'll call it late-binding).

Solution 1: Have your pages implement an interface:

a-
public interface INewHandler
Function NewFunc() as string
end interface

b-
Have your pages implement said interface
public class WebForm1
Inherits Page
Implements INewHandler

...
public function NewFunc() as string Implements INewHandler.NewFunc()
return "sample string"
end function

c -
Use the interface in your user control:
sub lbtNew_Click
lblMessage.Text = ctype(Page, INewHandler).NewFunc()
end sub



The other [good] solution would be to have your user control fire an event,
which each page could handle and then manipulate the user control as
necessary. i think the one I showed above is a better alternative, so I'll
forgo explaining it..

Karl
 
Pretty good solution, Karl, but I think he has a basic design flaw to his
method. That is, his Control now has an external dependency, which is the
function that exists (should exist) in the Page. This is not good modular
design. I believe that the control should have its own functionality (be
self-contained, with no external dependencies), and fire events. That way,
any client app that includes the Control can handle the events, and it
doesn't matter if nobody is listening.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

Karl Seguin said:
John,
Not sure I understand, so let me repeat your problem so that you can
confirm.

-You have a user control (ascx) that'll be placed on each page
-The user control has a new button
-When the new button is clicked, you want to call a function from the page
(aspx)
-Obviously page could be one of any aspx files which has the user control on
it

If this is your problem, there are 2 good solutions (and one not so good
that I won't go into, but we'll call it late-binding).

Solution 1: Have your pages implement an interface:

a-
public interface INewHandler
Function NewFunc() as string
end interface

b-
Have your pages implement said interface
public class WebForm1
Inherits Page
Implements INewHandler

...
public function NewFunc() as string Implements INewHandler.NewFunc()
return "sample string"
end function

c -
Use the interface in your user control:
sub lbtNew_Click
lblMessage.Text = ctype(Page, INewHandler).NewFunc()
end sub



The other [good] solution would be to have your user control fire an event,
which each page could handle and then manipulate the user control as
necessary. i think the one I showed above is a better alternative, so I'll
forgo explaining it..

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


grist2mill said:
I want to create a standard tool bar that appears on all pages that is
a control. The toolbar has a button 'New'. What I wolud like when the
user clicks on 'New' depends on the page they are on. I would like to
do this by defining a NewFunc() that is different in each
(code-behind) page which is called by the standard 'New' button in the
toolbar.

How can I get this to work? So far I have the following (which doesnt
work). When the user clicks 'New', I would like it to call NewFunc()
as defined on the current page that is presenting the user control.

Private Sub lbtNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtSearch.Click
lblMessage.Text = MyClass.Page.NewFunc()
'lblMessage.Text = MyClass.Parent.Page.NewFunc()
'lblMessage.Text = MyBase.Page.NewFunc()

End Sub

(in the above example, NewFunc simply returns a string to be displayed
on a label control on the toolbar)

Any suggestions appreciated.
regards
John
 
Kevin,
You are right, so I'll give the sample event code I recently gave to another
post we both replied to in hopes that it'll help John as well:

public class LeftMenu: WebControl{
public event CommandEventHandler NodeClick;
...
private void custExpand_Click(object sender,EventArgs e) {
m_CurrentNodeId = e.SomeStringValue;
if (this.NodeClick != null){
CommandEventArgs ev = new CommandEventArgs("clicked",
m_CurrentNodeId);
this.NodeClick(this, ev);
}
}
}

the in your page, you can:

page_load{
mmLeftMenu =
(LeftMenu)LoadControl(pnlLeftMenu,@"~\Controls\LeftMenu.ascx");
mmLeftMenu.NodeClick += new CommandEventHandler(mmLeftMenu_NodeClick);
}

private void mmLeftMenu_NodeClick(object sender, CommandEventArgs e) {
Response.Write("node value: " + e.CommandArgument);
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


Kevin Spencer said:
Pretty good solution, Karl, but I think he has a basic design flaw to his
method. That is, his Control now has an external dependency, which is the
function that exists (should exist) in the Page. This is not good modular
design. I believe that the control should have its own functionality (be
self-contained, with no external dependencies), and fire events. That way,
any client app that includes the Control can handle the events, and it
doesn't matter if nobody is listening.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

Karl Seguin said:
John,
Not sure I understand, so let me repeat your problem so that you can
confirm.

-You have a user control (ascx) that'll be placed on each page
-The user control has a new button
-When the new button is clicked, you want to call a function from the page
(aspx)
-Obviously page could be one of any aspx files which has the user
control
on
it

If this is your problem, there are 2 good solutions (and one not so good
that I won't go into, but we'll call it late-binding).

Solution 1: Have your pages implement an interface:

a-
public interface INewHandler
Function NewFunc() as string
end interface

b-
Have your pages implement said interface
public class WebForm1
Inherits Page
Implements INewHandler

...
public function NewFunc() as string Implements INewHandler.NewFunc()
return "sample string"
end function

c -
Use the interface in your user control:
sub lbtNew_Click
lblMessage.Text = ctype(Page, INewHandler).NewFunc()
end sub



The other [good] solution would be to have your user control fire an event,
which each page could handle and then manipulate the user control as
necessary. i think the one I showed above is a better alternative, so I'll
forgo explaining it..

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


grist2mill said:
I want to create a standard tool bar that appears on all pages that is
a control. The toolbar has a button 'New'. What I wolud like when the
user clicks on 'New' depends on the page they are on. I would like to
do this by defining a NewFunc() that is different in each
(code-behind) page which is called by the standard 'New' button in the
toolbar.

How can I get this to work? So far I have the following (which doesnt
work). When the user clicks 'New', I would like it to call NewFunc()
as defined on the current page that is presenting the user control.

Private Sub lbtNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtSearch.Click
lblMessage.Text = MyClass.Page.NewFunc()
'lblMessage.Text = MyClass.Parent.Page.NewFunc()
'lblMessage.Text = MyBase.Page.NewFunc()

End Sub

(in the above example, NewFunc simply returns a string to be displayed
on a label control on the toolbar)

Any suggestions appreciated.
regards
John
 
Thanks for your helpful suggestions. I'm eager to do this in the right
way (as I hope to reuse my control widely) but I have trouble
following Karl's sample (I'm not used to C#). This is what I have so
far (VB):

Public Event myClick As CommandEventHandler

Private Sub lbtSearch_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lbtSearch.Click
'lblMessage.Text = MyClass.Page.NewFunction()
Dim myString As String = "Fred"
If Not Me.myClick Is Nothing Then
Dim ev As CommandEventArgs = New
CommandEventArgs("Clicked", myString)
Me.myClick(Me, ev)
End If
End Sub

I get errors from the vs editor 'MyClick is an event and cannot be
called directly'.

Any tips or suggestions appreciated.
regards
John


Karl Seguin said:
Kevin,
You are right, so I'll give the sample event code I recently gave to another
post we both replied to in hopes that it'll help John as well:

public class LeftMenu: WebControl{
public event CommandEventHandler NodeClick;
...
private void custExpand_Click(object sender,EventArgs e) {
m_CurrentNodeId = e.SomeStringValue;
if (this.NodeClick != null){
CommandEventArgs ev = new CommandEventArgs("clicked",
m_CurrentNodeId);
this.NodeClick(this, ev);
}
}
}

the in your page, you can:

page_load{
mmLeftMenu =
(LeftMenu)LoadControl(pnlLeftMenu,@"~\Controls\LeftMenu.ascx");
mmLeftMenu.NodeClick += new CommandEventHandler(mmLeftMenu_NodeClick);
}

private void mmLeftMenu_NodeClick(object sender, CommandEventArgs e) {
Response.Write("node value: " + e.CommandArgument);
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


Kevin Spencer said:
Pretty good solution, Karl, but I think he has a basic design flaw to his
method. That is, his Control now has an external dependency, which is the
function that exists (should exist) in the Page. This is not good modular
design. I believe that the control should have its own functionality (be
self-contained, with no external dependencies), and fire events. That way,
any client app that includes the Control can handle the events, and it
doesn't matter if nobody is listening.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

Karl Seguin said:
John,
Not sure I understand, so let me repeat your problem so that you can
confirm.

-You have a user control (ascx) that'll be placed on each page
-The user control has a new button
-When the new button is clicked, you want to call a function from the page
(aspx)
-Obviously page could be one of any aspx files which has the user
control
on
it

If this is your problem, there are 2 good solutions (and one not so good
that I won't go into, but we'll call it late-binding).

Solution 1: Have your pages implement an interface:

a-
public interface INewHandler
Function NewFunc() as string
end interface

b-
Have your pages implement said interface
public class WebForm1
Inherits Page
Implements INewHandler

...
public function NewFunc() as string Implements INewHandler.NewFunc()
return "sample string"
end function

c -
Use the interface in your user control:
sub lbtNew_Click
lblMessage.Text = ctype(Page, INewHandler).NewFunc()
end sub



The other [good] solution would be to have your user control fire an event,
which each page could handle and then manipulate the user control as
necessary. i think the one I showed above is a better alternative, so I'll
forgo explaining it..

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


I want to create a standard tool bar that appears on all pages that is
a control. The toolbar has a button 'New'. What I wolud like when the
user clicks on 'New' depends on the page they are on. I would like to
do this by defining a NewFunc() that is different in each
(code-behind) page which is called by the standard 'New' button in the
toolbar.

How can I get this to work? So far I have the following (which doesnt
work). When the user clicks 'New', I would like it to call NewFunc()
as defined on the current page that is presenting the user control.

Private Sub lbtNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtSearch.Click
lblMessage.Text = MyClass.Page.NewFunc()
'lblMessage.Text = MyClass.Parent.Page.NewFunc()
'lblMessage.Text = MyBase.Page.NewFunc()

End Sub

(in the above example, NewFunc simply returns a string to be displayed
on a label control on the toolbar)

Any suggestions appreciated.
regards
John
 
In vb.net you use RaiseEvent:

Private Sub lbtSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'lblMessage.Text = MyClass.Page.NewFunction()
Dim myString As String = "Fred"
Dim ev As CommandEventArgs = New CommandEventArgs("Clicked", myString)
RaiseEvent myClick(Me, ev)
End Sub

and I don't _think_ you need to check if myClick is nothing....RaiseEvent
will protect against that....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


grist2mill said:
Thanks for your helpful suggestions. I'm eager to do this in the right
way (as I hope to reuse my control widely) but I have trouble
following Karl's sample (I'm not used to C#). This is what I have so
far (VB):

Public Event myClick As CommandEventHandler

Private Sub lbtSearch_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lbtSearch.Click
'lblMessage.Text = MyClass.Page.NewFunction()
Dim myString As String = "Fred"
If Not Me.myClick Is Nothing Then
Dim ev As CommandEventArgs = New
CommandEventArgs("Clicked", myString)
Me.myClick(Me, ev)
End If
End Sub

I get errors from the vs editor 'MyClick is an event and cannot be
called directly'.

Any tips or suggestions appreciated.
regards
John


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news: said:
Kevin,
You are right, so I'll give the sample event code I recently gave to another
post we both replied to in hopes that it'll help John as well:

public class LeftMenu: WebControl{
public event CommandEventHandler NodeClick;
...
private void custExpand_Click(object sender,EventArgs e) {
m_CurrentNodeId = e.SomeStringValue;
if (this.NodeClick != null){
CommandEventArgs ev = new CommandEventArgs("clicked",
m_CurrentNodeId);
this.NodeClick(this, ev);
}
}
}

the in your page, you can:

page_load{
mmLeftMenu =
(LeftMenu)LoadControl(pnlLeftMenu,@"~\Controls\LeftMenu.ascx");
mmLeftMenu.NodeClick += new CommandEventHandler(mmLeftMenu_NodeClick);
}

private void mmLeftMenu_NodeClick(object sender, CommandEventArgs e) {
Response.Write("node value: " + e.CommandArgument);
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


Kevin Spencer said:
Pretty good solution, Karl, but I think he has a basic design flaw to his
method. That is, his Control now has an external dependency, which is the
function that exists (should exist) in the Page. This is not good modular
design. I believe that the control should have its own functionality (be
self-contained, with no external dependencies), and fire events. That way,
any client app that includes the Control can handle the events, and it
doesn't matter if nobody is listening.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message John,
Not sure I understand, so let me repeat your problem so that you can
confirm.

-You have a user control (ascx) that'll be placed on each page
-The user control has a new button
-When the new button is clicked, you want to call a function from
the
page
(aspx)
-Obviously page could be one of any aspx files which has the user control
on
it

If this is your problem, there are 2 good solutions (and one not so good
that I won't go into, but we'll call it late-binding).

Solution 1: Have your pages implement an interface:

a-
public interface INewHandler
Function NewFunc() as string
end interface

b-
Have your pages implement said interface
public class WebForm1
Inherits Page
Implements INewHandler

...
public function NewFunc() as string Implements INewHandler.NewFunc()
return "sample string"
end function

c -
Use the interface in your user control:
sub lbtNew_Click
lblMessage.Text = ctype(Page, INewHandler).NewFunc()
end sub



The other [good] solution would be to have your user control fire an event,
which each page could handle and then manipulate the user control as
necessary. i think the one I showed above is a better alternative,
so
I'll
forgo explaining it..

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


I want to create a standard tool bar that appears on all pages that is
a control. The toolbar has a button 'New'. What I wolud like when the
user clicks on 'New' depends on the page they are on. I would like to
do this by defining a NewFunc() that is different in each
(code-behind) page which is called by the standard 'New' button in the
toolbar.

How can I get this to work? So far I have the following (which doesnt
work). When the user clicks 'New', I would like it to call NewFunc()
as defined on the current page that is presenting the user control.

Private Sub lbtNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtSearch.Click
lblMessage.Text = MyClass.Page.NewFunc()
'lblMessage.Text = MyClass.Parent.Page.NewFunc()
'lblMessage.Text = MyBase.Page.NewFunc()

End Sub

(in the above example, NewFunc simply returns a string to be displayed
on a label control on the toolbar)

Any suggestions appreciated.
regards
John
 
Thanks for your help. This was not nearly as easy as I'd hoped. But
here's my current solution. This example simply has two buttons that
change the text of a label

1. in the code behind to the user control you need the following for
each button to raise the event message:
==========================================
'RESET BUTTON
'Delegate Declaration
Public Delegate Sub myResetHandler(ByVal sender As Object, ByVal e
As EventArgs)
'Event declaration
Public Event myReset As myEventHandler
'Raise the event by invoking the delegate, sending "me", the
current instance of the class
Protected Sub OnMyReset(ByVal e As EventArgs)
RaiseEvent myReset(Me, e)
End Sub
'Handle the Button
Private Sub lbtReset_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lbtReset.Click
Dim evt As New EventArgs()
OnMyReset(evt)
End Sub

2. Then when you declare the user control in your form
you must link the events raised by the buttons to methods in the web
form
==========================================

<P><uc1:basicevent id="MyEvent" runat="server" OnMyEvent="SetMyText"
OnMyReset="ResetText"></uc1:basicevent></P>
<P><asp:label id="lblPageLabel" runat="server">Pagelabel: Default
Text</asp:label></P>

3. Then in the aspx.vb code behind page you need to provide the
functions that are being called:
==========================================
Sub SetMyText(ByVal sender As Object, ByVal e As EventArgs)
lblPageLabel.Text = "Changed"
End Sub

Sub ResetText(ByVal sender As Object, ByVal e As EventArgs)
lblPageLabel.Text = "Default"
End Sub

Phew! glad I finally got that to work! thanks for your tips that
started me looking in the right direction.
regards
John




Karl said:
In vb.net you use RaiseEvent:

Private Sub lbtSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'lblMessage.Text = MyClass.Page.NewFunction()
Dim myString As String = "Fred"
Dim ev As CommandEventArgs = New CommandEventArgs("Clicked", myString)
RaiseEvent myClick(Me, ev)
End Sub

and I don't _think_ you need to check if myClick is nothing....RaiseEvent
will protect against that....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


grist2mill said:
Thanks for your helpful suggestions. I'm eager to do this in the right
way (as I hope to reuse my control widely) but I have trouble
following Karl's sample (I'm not used to C#). This is what I have so
far (VB):

Public Event myClick As CommandEventHandler

Private Sub lbtSearch_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lbtSearch.Click
'lblMessage.Text = MyClass.Page.NewFunction()
Dim myString As String = "Fred"
If Not Me.myClick Is Nothing Then
Dim ev As CommandEventArgs = New
CommandEventArgs("Clicked", myString)
Me.myClick(Me, ev)
End If
End Sub

I get errors from the vs editor 'MyClick is an event and cannot be
called directly'.

Any tips or suggestions appreciated.
regards
John


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news: said:
Kevin,
You are right, so I'll give the sample event code I recently gave to another
post we both replied to in hopes that it'll help John as well:

public class LeftMenu: WebControl{
public event CommandEventHandler NodeClick;
...
private void custExpand_Click(object sender,EventArgs e) {
m_CurrentNodeId = e.SomeStringValue;
if (this.NodeClick != null){
CommandEventArgs ev = new CommandEventArgs("clicked",
m_CurrentNodeId);
this.NodeClick(this, ev);
}
}
}

the in your page, you can:

page_load{
mmLeftMenu =
(LeftMenu)LoadControl(pnlLeftMenu,@"~\Controls\LeftMenu.ascx");
mmLeftMenu.NodeClick += new CommandEventHandler(mmLeftMenu_NodeClick);
}

private void mmLeftMenu_NodeClick(object sender, CommandEventArgs e) {
Response.Write("node value: " + e.CommandArgument);
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


Pretty good solution, Karl, but I think he has a basic design flaw to his
method. That is, his Control now has an external dependency, which is the
function that exists (should exist) in the Page. This is not good modular
design. I believe that the control should have its own functionality (be
self-contained, with no external dependencies), and fire events. That way,
any client app that includes the Control can handle the events, and it
doesn't matter if nobody is listening.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message John,
Not sure I understand, so let me repeat your problem so that you can
confirm.

-You have a user control (ascx) that'll be placed on each page
-The user control has a new button
-When the new button is clicked, you want to call a function from
the
page
(aspx)
-Obviously page could be one of any aspx files which has the user
control
on
it

If this is your problem, there are 2 good solutions (and one not so good
that I won't go into, but we'll call it late-binding).

Solution 1: Have your pages implement an interface:

a-
public interface INewHandler
Function NewFunc() as string
end interface

b-
Have your pages implement said interface
public class WebForm1
Inherits Page
Implements INewHandler

...
public function NewFunc() as string Implements INewHandler.NewFunc()
return "sample string"
end function

c -
Use the interface in your user control:
sub lbtNew_Click
lblMessage.Text = ctype(Page, INewHandler).NewFunc()
end sub



The other [good] solution would be to have your user control fire an event,
which each page could handle and then manipulate the user control as
necessary. i think the one I showed above is a better alternative,
so
I'll
forgo explaining it..

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


I want to create a standard tool bar that appears on all pages that is
a control. The toolbar has a button 'New'. What I wolud like when the
user clicks on 'New' depends on the page they are on. I would like to
do this by defining a NewFunc() that is different in each
(code-behind) page which is called by the standard 'New' button in the
toolbar.

How can I get this to work? So far I have the following (which doesnt
work). When the user clicks 'New', I would like it to call NewFunc()
as defined on the current page that is presenting the user control.

Private Sub lbtNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtSearch.Click
lblMessage.Text = MyClass.Page.NewFunc()
'lblMessage.Text = MyClass.Parent.Page.NewFunc()
'lblMessage.Text = MyBase.Page.NewFunc()

End Sub

(in the above example, NewFunc simply returns a string to be displayed
on a label control on the toolbar)

Any suggestions appreciated.
regards
John
 
Back
Top