Handle Event of Dynamically Added User Control

Discussion in 'ASP .Net Building Controls' started by Jon B, Apr 29, 2005.

  1. Jon B

    Jon B Guest

    Hi There!

    How to handle the events of a dynamically added user control?

    e.g. I have following code...

    Dim myUserControl as Object = LoadControl("myFirstControl.ascx")

    myFirstControl fires LinkClicked event and I don't know how to handle that
    LinkClicked event from containing page.

    Please help!! Thanks!!!

    Jon
    Jon B, Apr 29, 2005
    #1
    1. Advertising

  2. Jon B

    Guest

    You will need to use addhandler to specify the event handler as in


    Dim myUserControl as Object = LoadControl("myFirstControl.ascx")
    AddHandler myUserControl.myEvent, AddressOf Me.MyEventHandler

    Elsewhere on the page place the event handling routing you specified

    Sub MyEventHandler(obj as Object, e as EventArgs)
    'Do something when the event fires
    End Sub

    Bill E.
    Hollywood, FL




    Jon B wrote:
    > Hi There!
    >
    > How to handle the events of a dynamically added user control?
    >
    > e.g. I have following code...
    >
    > Dim myUserControl as Object = LoadControl("myFirstControl.ascx")
    >
    > myFirstControl fires LinkClicked event and I don't know how to handle

    that
    > LinkClicked event from containing page.
    >
    > Please help!! Thanks!!!
    >
    > Jon
    , Apr 29, 2005
    #2
    1. Advertising

  3. Jon B

    Jon B Guest

    Thanks for the reply Bill, but I get the following error message when I
    tried your method...

    BC30676: 'LinkClicked' is not an event of 'System.Object'.

    Any ideas? Thanks again!!!

    Jon

    <> wrote in message
    news:...
    > You will need to use addhandler to specify the event handler as in
    >
    >
    > Dim myUserControl as Object = LoadControl("myFirstControl.ascx")
    > AddHandler myUserControl.myEvent, AddressOf Me.MyEventHandler
    >
    > Elsewhere on the page place the event handling routing you specified
    >
    > Sub MyEventHandler(obj as Object, e as EventArgs)
    > 'Do something when the event fires
    > End Sub
    >
    > Bill E.
    > Hollywood, FL
    >
    >
    >
    >
    > Jon B wrote:
    >> Hi There!
    >>
    >> How to handle the events of a dynamically added user control?
    >>
    >> e.g. I have following code...
    >>
    >> Dim myUserControl as Object = LoadControl("myFirstControl.ascx")
    >>
    >> myFirstControl fires LinkClicked event and I don't know how to handle

    > that
    >> LinkClicked event from containing page.
    >>
    >> Please help!! Thanks!!!
    >>
    >> Jon

    >
    Jon B, Apr 29, 2005
    #3
  4. Jon B

    Guest

    Well, I suppose that you need to dimension your user control not as an
    object, but as an instance of the user control's type

    Dim MyUserControl as usercontrolclass =
    LoadControl("myFirstControl.ascx")

    Bill
    , Apr 29, 2005
    #4
  5. Jon B

    Jon B Guest

    Thanks for the reply again Bill! But how do I dimension my user control as
    the type of User control?

    I'm not using Code-Behind method and my project also didn't get complied
    every time I change something. Both code and HTML is in one ASCX file.
    There's a ClassName attribute at the top of the page in Control directive.
    But I'm unable to declare an object with that type in containing page.

    So how do I go about it??

    Thanks!!
    Jon

    <> wrote in message
    news:...
    > Well, I suppose that you need to dimension your user control not as an
    > object, but as an instance of the user control's type
    >
    > Dim MyUserControl as usercontrolclass =
    > LoadControl("myFirstControl.ascx")
    >
    > Bill
    >
    Jon B, Apr 29, 2005
    #5
  6. Jon B

    Guest

    Jon,

    Now I understand. Things are a bit less clear with the in-line coding.

    If you don't use the ClassName attribute in your control
    "myusercontrol.ascx", I think that the class is implicitly created as

    ASP.myusercontrol_ascx

    If you specify a class name, it will be

    ASP.myspecifiedclassname

    However, if you're unsure, you can load the control as an object, then
    return its type with GetType as in

    Dim myUserControl as Object = LoadControl("myFirstControl.ascx")
    Response.Write(myUserControl.GetType.toString)

    Once you know the type, you can change to

    Dim myUserControl as ASP.myFirstControl =
    LoadControl("myFirstControl.ascx")
    AddHandler myUserControl.myCustomEvent, AddressOf Me.MyEventHandler

    Bill
    , Apr 30, 2005
    #6
    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. Umut Tezduyar
    Replies:
    1
    Views:
    400
    Steven Cheng[MSFT]
    Oct 6, 2004
  2. Bennett Haselton
    Replies:
    1
    Views:
    514
    John M Deal
    Nov 8, 2004
  3. Jon B
    Replies:
    5
    Views:
    5,210
  4. niberhate
    Replies:
    1
    Views:
    1,341
    Cubaman
    Apr 26, 2010
  5. Umut Tezduyar
    Replies:
    8
    Views:
    237
    Steven Cheng[MSFT]
    Oct 8, 2004
Loading...

Share This Page