Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: Raise Event from Dynamically loaded Web User Control

Reply
Thread Tools

Re: Raise Event from Dynamically loaded Web User Control

 
 
wfairl@gmail.com
Guest
Posts: n/a
 
      12-15-2006
You need to declare the ctl as your derived type and explicity cast the
return value from LoadControl.

I don't use VB.net but I believe it would be

Dim ctl as MyUserControl =
DirectCast(LoadControl("myWebUserControl.ascx"), MyUserControl)

Angel wrote:
> I created a web user control(.ascx) that I load dynamically on my web page.
> In this web user control I have an event that I want to expose for my page so
> it can capture it and perform certain functions. Unfortunately I cannot
> addhandler on my page to the dynamically created web user control.
>
> On Web User Control
>
> Partial Class myWebUserControl
> Inherits System.Web.UI.UserControl
>
>
> Public Event SetFilter(ByVal FilterArgs As
> KHE.Orion.Business.Components.FinancialAid.FAR.Rep orts.FilterArgs)
>
>
> Sub Whatever()
> RaiseEvent SetFilter(myArgs)
> End Sub
>
> End Class
>
> On the Web Page:
> Partial Class MyPage
> Inherits System.Web.UI.Page
>
> Sub LoadControl()
> Dim ctl as Control = LoadControl("myWebUserControl.ascx")
> Page.controls.add(ctl)
>
> 'This code will not work because it does not know that there is an event
> because the control is declared as a control
> AddHandler ctl.SetFilter, AddressOf mySetFilter
>
> End Sub
>
> Sub mySetFilter()
>
> End Sub
>
> End Class
>
> I cannot capture the setFilter event in the page because the declaration of
> the control is dynamically added as a Control. I need to have this
> WebUserControl added dynamically. So How can I notify the parent that
> something happened in the WebUserControl?
>
> Thanks in advance...


 
Reply With Quote
 
 
 
 
wfairl@gmail.com
Guest
Posts: n/a
 
      12-15-2006
Angel wrote:
> Unfortunately the WebUserControl is not exposed so that you can declare it..
> So there must be some other way. Anyone...
>
> Thanks though
>


I'm not sure I understand exactly what you mean by it's "not exposed".
Is this a 2005 web project by any chance (does it have an app_code
folder)? If so you can either upgrade to the 2005 web application
project
http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx
or work around the problem of not being able to pick up your codebehind
types at compile time by creating separate "stub" classes, placing them
in your app_code folder, and having your codebehind classes derive from
them. Like this:

Stub File:

Public Class _myWebUserControl
Inherits System.Web.UI.UserControl


Public Event SetFilter(ByVal FilterArgs As
KHE.Orion.Business.Components.FinancialAid.FAR.Rep orts.FilterArgs)

CodeBehind file:

Public Class myWebUserControl
Inherits _myWebUserControl

Sub Whatever()
RaiseEvent SetFilter(myArgs)
End Sub

On the Web Page:

Partial Class MyPage
Inherits System.Web.UI.Page


Sub LoadControl()
Dim ctl as _myWebUserControl =
CType(LoadControl("myWebUserControl.ascx"),_myWebU serControl)

Page.controls.add(ctl)

AddHandler ctl.SetFilter, AddressOf mySetFilter

 
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 handling of dynamically loaded user control JB McMichael ASP .Net 3 08-04-2005 04:07 AM
Event fails to fire first time in dynamically loaded user control( =?Utf-8?B?U3RldmUgQm9vdGg=?= ASP .Net 6 04-06-2005 07:53 PM
FWD: Help: Control lost doesn't raise event after being moved to dynamically loaded user control Tonix ASP .Net Web Controls 0 08-24-2004 05:42 AM
FWD: Help: Control lost doesn't raise event after being moved to dynamically loaded user control Tonix ASP .Net 0 08-24-2004 05:26 AM
Help: Control lost doesn't raise event after being moved to dynamically loaded user control Thanh Nguyen ASP .Net 0 04-01-2004 02:23 AM



Advertisments