Calling a function from a user control

I

IntraRELY

I have a user control which contains a button. In the btn click event
handler I need to call a funtion in the parent aspx page, but not sure how
to do it.

TIA,

Steve
 
M

mortb

You colud define an event on the user control and add a event handler on the
container page.
When the user control button is clicked, run any added events.

cheers,
mortb
 
I

IntraRELY

Thanks, but am a bit lost. Here this Event Handler:

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
' Call the aspx funtion ok()
End Sub

Here is a snipped from the aspx page.
Public Sub OK() Handles ucManageButtons.Load with events
lblMessage.Text = "Test OK"
End Sub

I am not sure how to code this???

TIA,

Steve

Leon Friesema said:
I have a user control which contains a button. In the btn click event
handler I need to call a funtion in the parent aspx page, but not sure
how
to do it.

TIA,

Steve

(([ParentObjectType])this.Parent).[Funtion]([Parameters]);

With the parentobjecttype you cast the object "parent" to the
objecttype from your parent. After that you can call any public method
in the class.

Leon.
 
I

IntraRELY

Thanks, I was ok with the c#, it's very easy to translate. I didnt
understand what ParentObjectType was. Anyways, The coded looked like it
worked, but when the page runs, I get the error "Specified cast is not
valid. ".

I developed a solution, but had issues with it. Here is what I did:

onClick event in usercontrol...
Dim page As Manage.parentPage = New parentPage
page.OK()

Public Sub OK()
Me.lblMessage.Text = "Test" ' Note: also removed Me. with same effect.
End Sub

Here is the error message:
Object reference not set to an instance of an object.

If I replace the Me.lblMessage.Text with
"HttpContext.Current.Response.Write("Hello")" it works ok.

So I was able to call it and run it, however, it looks like there is an
issue with the context I am in.

Thanks for you help,

Steve

Leon Friesema said:
Leon Friesema said:
I have a user control which contains a button. In the btn click event
handler I need to call a funtion in the parent aspx page, but not sure
how
to do it.

TIA,

Steve


(([ParentObjectType])this.Parent).[Funtion]([Parameters]);

With the parentobjecttype you cast the object "parent" to the
objecttype from your parent. After that you can call any public method
in the class.

Leon.

Thanks, but am a bit lost. Here this Event Handler:

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
' Call the aspx funtion ok()
End Sub

Here is a snipped from the aspx page.
Public Sub OK() Handles ucManageButtons.Load with events
lblMessage.Text = "Test OK"
End Sub

I am not sure how to code this???

TIA,

Steve

Wow.. VB.NET, I answered in C# syntax, no wonder you got lost.. Well
some digging in the MSDN brought up the CType-method, so let's take it
from there.
Furthermore: you're mixing up 2 things; eventhandling and
method-calling; You state Sub Ok is handling events from the
UserControl, which it isn't, it's just method;

Let's say your parent page, containing the control is named
"ParentPage.aspx", with that your class name would be "ParentPage",
so..

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
CType(Me.Parent, ParentPage).Ok()
End Sub

Here is a snipped from the aspx page.
Public Sub OK()
' --> FORGET THIS: Handles ucManageButtons.Load with events
lblMessage.Text = "Test OK"
End Sub

Leon.
 
I

IntraRELY

As you can see lblMessage is within OK() and this function is in the aspx
page. The user control is on the aspx page as well.

Leon Friesema said:
Thanks, I was ok with the c#, it's very easy to translate. I didnt
understand what ParentObjectType was. Anyways, The coded looked like it
worked, but when the page runs, I get the error "Specified cast is not
valid. ".

I developed a solution, but had issues with it. Here is what I did:

onClick event in usercontrol...
Dim page As Manage.parentPage = New parentPage
page.OK()

Public Sub OK()
Me.lblMessage.Text = "Test" ' Note: also removed Me. with same effect.
End Sub

Here is the error message:
Object reference not set to an instance of an object.

If I replace the Me.lblMessage.Text with
"HttpContext.Current.Response.Write("Hello")" it works ok.

So I was able to call it and run it, however, it looks like there is an
issue with the context I am in.

Thanks for you help,

Steve

Leon Friesema said:
I have a user control which contains a button. In the btn click event
handler I need to call a funtion in the parent aspx page, but not
sure
how
to do it.

TIA,

Steve


(([ParentObjectType])this.Parent).[Funtion]([Parameters]);

With the parentobjecttype you cast the object "parent" to the
objecttype from your parent. After that you can call any public method
in the class.

Leon.

Thanks, but am a bit lost. Here this Event Handler:

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
' Call the aspx funtion ok()
End Sub

Here is a snipped from the aspx page.
Public Sub OK() Handles ucManageButtons.Load with events
lblMessage.Text = "Test OK"
End Sub

I am not sure how to code this???

TIA,

Steve

Wow.. VB.NET, I answered in C# syntax, no wonder you got lost.. Well
some digging in the MSDN brought up the CType-method, so let's take it
from there.
Furthermore: you're mixing up 2 things; eventhandling and
method-calling; You state Sub Ok is handling events from the
UserControl, which it isn't, it's just method;

Let's say your parent page, containing the control is named
"ParentPage.aspx", with that your class name would be "ParentPage",
so..

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
CType(Me.Parent, ParentPage).Ok()
End Sub

Here is a snipped from the aspx page.
Public Sub OK()
' --> FORGET THIS: Handles ucManageButtons.Load with events
lblMessage.Text = "Test OK"
End Sub

Leon.

Where is the lblMessage? In which control, on what page? That's what's
wrong.

Leon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top