Only one instance of custom control can register in page like scriptmanager

39 Views Asked by At

I am creating a custom web control that I need only one instance of control user can add in page, same like scriptmanager.

I need this check under custom control it self but not getting any correct approach, I know by iterating page.controls property I can check existence of control but I would like to know is it only a way? or is there any other generic/right way to check like scriptmanager does when more than one instance found in page.

1

There are 1 best solutions below

0
levent On

First thing come to my mind..

on your custom control..

    protected override void OnInit(EventArgs e)
    {
        if (Context.Items.Contains("MyCustomControl"))
            throw new Exception("only one instance of a MyCustomControl can be added to the page");
        Context.Items["MyCustomControl"] = true;
        base.OnInit(e);

    }