Solved

I think I need to override a user control's host page event? How ?

Posted on 2011-03-22
3
741 Views
Last Modified: 2012-05-11
Hi,

I have a Page_loadComplete event in the Host page of a User Control. However for 3 out of 10 User controls I need to ignore the host page's implementation of this event and use a specific version for the 3 user controls in question.

My first thought was to code an "Overrides" event in the User Control CBH? But not sure if this is the way to do this and how?

Thoughts and code greatly appreciated?

Thanks,

Sam
'Host Page code
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

'Standard code here

End Sub

'User control code idea, possibly... Does not work.
Protected Overrides Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

'Custom User Control Code here, ignoring/Overriding Standard Host Page code.

End Sub

Open in new window

0
Comment
Question by:SamJolly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35188703
You won't be able to "ignore" the host page's Page_LoadComplete using any mechanism from within the UserControl--they are two different classes. What you can do is go to your hosting page and create the Overrides OnLoadComplete() method. Inside of this sub, be sure not to call MyBase.OnLoadComplete.

Protected Overrides Sub OnLoadComplete(ByVal e As System.EventArgs)
End Sub

Open in new window


Now if you are sharing the host page between several of these user controls (using LoadControl() to include the user control into your page), then you'll need to a little bit of work. You *could* create a Shared boolean member in your hosting page that serves as a flag to indicate whether or not to ignore calling the hosting page's OnLoadComplete() method:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Private Shared _ignoreLoadComplete As Boolean

    Protected Overrides Sub OnLoadComplete(ByVal e As System.EventArgs)
        If Not _ignoreLoadComplete Then
            MyBase.OnLoadComplete(e)
        End If
    End Sub

    Public Shared Property IgnoreLoadComplete() As Boolean
        Get
            Return _ignoreLoadComplete
        End Get
        Set(ByVal value As Boolean)
            _ignoreLoadComplete = value
        End Set
    End Property

End Class

Open in new window


Then, in the user control's you want to ignore, you can set this value inside of any of the events that occur prior to LoadComplete. OnLoad() would be one example:

Public Partial Class WebUserControl1
    Inherits System.Web.UI.UserControl

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        _Default.IgnoreLoadComplete = True

        MyBase.OnLoad(e)
    End Sub
End Class

Open in new window


Just be sure to reset the value for any pages that you don't want to ignore LoadComplete!
0
 

Author Closing Comment

by:SamJolly
ID: 35189783
thks. Great help although I ended up doing a variation on the theme!!! However you got me going in the right direction. I ended up using a flag "Property" from a business class which defaulted to false and the PageLoaded code always checked this flag before deciding what to do. THe flag was then always set back to false thus ensuring only the exception produced the different processing. Thanks again for your prompt and grt help.
Sam
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35189847
NP. Glad to help  = )
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question