2017-08-11 60 views
-1

我正在創建一個自定義Web控件,我只需要一個控件用戶可以在頁面中添加一個實例,就像scriptmanager一樣。只有一個自定義控件實例可以在頁面中註冊,就像scriptmanager

我需要這個檢查下自定義控制它自我但沒有得到任何正確的方法,我知道通過迭代page.controls屬性我可以檢查存在的控制,但我想知道它只是一種方式?或者是否有任何其他通用/正確的方式來檢查,如scriptmanager在頁面中找到多個實例時會執行此操作。

回答

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

    } 
相關問題