-1

我正在创建一个自定义 Web 控件,我只需要一个用户可以在页面中添加的控件实例,就像scriptmanager.

我需要在自定义控件下进行此检查,但没有得到任何正确的方法,我知道通过迭代 page.controls 属性我可以检查控件的存在,但我想知道它只是一种方法吗?scriptmanager或者在页面中找到多个实例时,是否有任何其他通用/正确的方法来检查类似的情况。

4

1 回答 1

0

我首先想到的是..

在您的自定义控件上..

    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);

    }
于 2017-08-11T14:47:02.493 回答