Solved

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

Posted on 2011-03-22
3
722 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now