开发者

Is User Control in Asp.Net can be completely self contain?

开发者 https://www.devze.com 2023-02-11 08:27 出处:网络
I am trying to make a UserControl that have all data process, initialize data, ajax, a开发者_Go百科ll self contain into one control.

I am trying to make a UserControl that have all data process, initialize data, ajax, a开发者_Go百科ll self contain into one control. So I can insert to anywhere on my Asp pages.

But the problem is, the UserControl contains a Script Manager and the Page that contain that User Control has a Script Manager too, so Asp doesn't allow me to have two Script Manager in one page.

In case like this, I wonder if UserControl can completely self contain? Or the proper way of using UserControl is just using it like a template, and all the data process, event handler I have to do on the page that contains the UserControl?

Thanks.

======Edit: I guess I will just use ScriptManager.GetCurrent to check if page exists ScriptManager and add it otherwise. Thanks.


For the most part, you can make it self-contained. Just use the ClientScript object. Something like this:

if (!Page.ClientScript.IsClientScriptBlockRegistered("key"))
{
    Page.ClientScript.RegisterClientScriptBlock(GetType(), "key",
        "function MyFunction() {" +
        "..." +
        "}", true);
}

By providing a unique key for your code block, you can ensure a particular block of javascript is added to the page once, and only once.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号