Re: Raise Event from Dynamically loaded Web User Control

Discussion in 'ASP .Net' started by wfairl@gmail.com, Dec 15, 2006.

  1. Guest

    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.Reports.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...
     
    , Dec 15, 2006
    #1
    1. Advertising

  2. Guest

    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.Reports.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"),_myWebUserControl)

    Page.controls.add(ctl)

    AddHandler ctl.SetFilter, AddressOf mySetFilter
     
    , Dec 15, 2006
    #2
    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. Thanh Nguyen
    Replies:
    0
    Views:
    446
    Thanh Nguyen
    Apr 1, 2004
  2. Tonix
    Replies:
    0
    Views:
    423
    Tonix
    Aug 24, 2004
  3. =?Utf-8?B?U3RldmUgQm9vdGg=?=

    Event fails to fire first time in dynamically loaded user control(

    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    594
    =?Utf-8?B?U3RldmUgQm9vdGg=?=
    Apr 6, 2005
  4. Tonix
    Replies:
    0
    Views:
    177
    Tonix
    Aug 24, 2004
  5. Marc Bogaard
    Replies:
    1
    Views:
    92
    john henry bonham
    Sep 17, 2004
Loading...

Share This Page