Professional Web Applications Themes

Handle Event of Dynamically Added User Control - ASP.NET Building Controls

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...

  1. #1

    Default Handle Event of Dynamically Added User Control

    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 Guest

  2. #2

    Default Re: Handle Event of Dynamically Added User Control

    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
    billmiami2@netscape.net Guest

  3. #3

    Default Re: Handle Event of Dynamically Added User Control

    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

    <billmiami2netscape.net> wrote in message
    news:1114770120.624232.293120o13g2000cwo.googlegr oups.com...
    > 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 Guest

  4. #4

    Default Re: Handle Event of Dynamically Added User Control

    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

    billmiami2@netscape.net Guest

  5. #5

    Default Re: Handle Event of Dynamically Added User Control


    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

    <billmiami2netscape.net> wrote in message
    news:1114779666.871704.230040o13g2000cwo.googlegr oups.com...
    > 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 Guest

  6. #6

    Default Re: Handle Event of Dynamically Added User Control

    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

    billmiami2@netscape.net Guest

Similar Threads

  1. Dynamically Adding User Control with Child User Control
    By Travis Pruitt in forum ASP.NET Building Controls
    Replies: 0
    Last Post: January 13th, 08:49 PM
  2. How to find control dynamically added in the header
    By Amit Chudasama in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: August 25th, 12:34 PM
  3. Replies: 5
    Last Post: February 24th, 02:07 PM
  4. Replies: 2
    Last Post: November 26th, 05:08 AM
  5. onclick event for dynamically added controls?
    By Alvin Bruney in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: July 27th, 04:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139