Solved

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

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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 74

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

8 Experts available now in Live!

Get 1:1 Help Now