markzzz
markzzz

Reputation: 47995

Can I take control of some WebForm from the external?

I have some WebForms, such as First.ascx.cs, Second.ascx.cs, Third.ascx.cs and so on!

Well, I'd like to call a function (let's say, startFunction()) at the PreInit stage, and another one (let's say, endFunction()) and the PreRender stage, for EACH context.

So:

startFunction();
... First.ascx.cs PageLoad execution...
endFunction();

startFunction();
... Second.ascx.cs PageLoad execution...
endFunction();

startFunction();
... Third.ascx.cs PageLoad execution...
endFunction();

without write the same start/end function and copy and paste for each context I need to control. Is there a good strategy with .NET (3.5) and WebForms?

Upvotes: 0

Views: 77

Answers (1)

giammin
giammin

Reputation: 18958

Inheritance!

Create a basecontrol where you attach to those events and then derive from it.

MarkzzzClass .cs

public abstract class MarkzzzClass : System.Web.UI.UserControl
{
//do something
}

BaseControl.cs:

public abstract class BaseControl : MarkzzzClass 
{
    protected override void OnPreRender(EventArgs e)
    {
        EndFunction();
        base.OnPreRender(e);
    }
    protected override void OnInit(EventArgs e)
    {
        StartFunction();
        base.OnInit(e);
    }
}

First.ascx.cs:

public partial class First : BaseControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Upvotes: 1

Related Questions