Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to limit User Input 2 27
FInd Image Control Gridview 3 21
Testing of the web services 1 34
VB.NET Inline If statement 4 15
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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