Calling a function from a user control

Discussion in 'ASP .Net Web Controls' started by IntraRELY, Nov 30, 2004.

  1. IntraRELY

    IntraRELY Guest

    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
     
    IntraRELY, Nov 30, 2004
    #1
    1. Advertising

  2. IntraRELY

    mortb Guest

    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" <> wrote in message
    news:...
    >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
     
    mortb, Nov 30, 2004
    #2
    1. Advertising

  3. IntraRELY

    IntraRELY Guest

    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:...
    > On Mon, 29 Nov 2004 20:30:16 -0800, "IntraRELY" <>
    > 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.
     
    IntraRELY, Nov 30, 2004
    #3
  4. IntraRELY

    IntraRELY Guest

    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:...
    > On Tue, 30 Nov 2004 13:33:44 -0800, "IntraRELY" <>
    > wrote:
    >
    >
    >>
    >>"Leon Friesema" <leon@@frostbits.nl> wrote in message
    >>news:...
    >>> On Mon, 29 Nov 2004 20:30:16 -0800, "IntraRELY" <>
    >>> 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.
     
    IntraRELY, Dec 1, 2004
    #4
  5. IntraRELY

    IntraRELY Guest

    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:...
    > On Wed, 1 Dec 2004 12:46:37 -0800, "IntraRELY" <>
    > 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:...
    >>> On Tue, 30 Nov 2004 13:33:44 -0800, "IntraRELY" <>
    >>> wrote:
    >>>
    >>>
    >>>>
    >>>>"Leon Friesema" <leon@@frostbits.nl> wrote in message
    >>>>news:...
    >>>>> On Mon, 29 Nov 2004 20:30:16 -0800, "IntraRELY" <>
    >>>>> 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.
     
    IntraRELY, Dec 5, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    0
    Views:
    647
    Mad Scientist Jr
    Mar 22, 2006
  2. Andreas Lagemann
    Replies:
    8
    Views:
    488
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    447
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    558
    Jonathan Wood
    Feb 2, 2010
  5. Replies:
    0
    Views:
    665
Loading...

Share This Page