Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Calling a function from a user control

Reply
Thread Tools

Calling a function from a user control

 
 
IntraRELY
Guest
Posts: n/a
 
      11-30-2004
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


 
Reply With Quote
 
 
 
 
mortb
Guest
Posts: n/a
 
      11-30-2004
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

"IntraRELY" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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



 
Reply With Quote
 
 
 
 
IntraRELY
Guest
Posts: n/a
 
      11-30-2004
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" <leon@@frostbits.nl> wrote in message
news:(E-Mail Removed)...
> On Mon, 29 Nov 2004 20:30:16 -0800, "IntraRELY" <(E-Mail Removed)>
> wrote:
>
>>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.



 
Reply With Quote
 
IntraRELY
Guest
Posts: n/a
 
      12-01-2004
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" <leon@@frostbits.nl> wrote in message
news:(E-Mail Removed)...
> On Tue, 30 Nov 2004 13:33:44 -0800, "IntraRELY" <(E-Mail Removed)>
> wrote:
>
>
>>
>>"Leon Friesema" <leon@@frostbits.nl> wrote in message
>>news:(E-Mail Removed). ..
>>> On Mon, 29 Nov 2004 20:30:16 -0800, "IntraRELY" <(E-Mail Removed)>
>>> wrote:
>>>
>>>>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.



 
Reply With Quote
 
IntraRELY
Guest
Posts: n/a
 
      12-05-2004
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" <leon@@frostbits.nl> wrote in message
news:(E-Mail Removed)...
> On Wed, 1 Dec 2004 12:46:37 -0800, "IntraRELY" <(E-Mail Removed)>
> wrote:
>
>>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" <leon@@frostbits.nl> wrote in message
>>news:(E-Mail Removed). ..
>>> On Tue, 30 Nov 2004 13:33:44 -0800, "IntraRELY" <(E-Mail Removed)>
>>> wrote:
>>>
>>>
>>>>
>>>>"Leon Friesema" <leon@@frostbits.nl> wrote in message
>>>>news:(E-Mail Removed) m...
>>>>> On Mon, 29 Nov 2004 20:30:16 -0800, "IntraRELY" <(E-Mail Removed)>
>>>>> wrote:
>>>>>
>>>>>>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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control to Control Other User Control Jonathan Wood ASP .Net 4 02-02-2010 03:41 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM



Advertisments