Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Handle Event of Dynamically Added User Control

Reply
Thread Tools

Handle Event of Dynamically Added User Control

 
 
Jon B
Guest
Posts: n/a
 
      04-29-2005
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


 
Reply With Quote
 
 
 
 
billmiami2@netscape.net
Guest
Posts: n/a
 
      04-29-2005
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


 
Reply With Quote
 
 
 
 
Jon B
Guest
Posts: n/a
 
      04-29-2005
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

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

>



 
Reply With Quote
 
billmiami2@netscape.net
Guest
Posts: n/a
 
      04-29-2005
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

 
Reply With Quote
 
Jon B
Guest
Posts: n/a
 
      04-29-2005

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

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



 
Reply With Quote
 
billmiami2@netscape.net
Guest
Posts: n/a
 
      04-30-2005
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

 
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
Event of a dynamically added user control doesn't fire niberhate ASP .Net 1 04-26-2010 04:03 PM
reference web control in dynamically added user control, ASP.NET 2 =?Utf-8?B?U2FtdWVs?= ASP .Net 7 08-10-2005 02:12 AM
Handle Event of Dynamically Added User Control Jon B ASP .Net 5 04-30-2005 12:33 PM
How can i handle post back event of the control that is added on the OnPreRender () method Umut Tezduyar ASP .Net Web Controls 8 10-08-2004 10:03 AM
How can i handle post back event of the control that is added on the OnPreRender () method Umut Tezduyar ASP .Net 1 10-06-2004 09:10 AM



Advertisments