手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

UpdatePanel和自定义控件中的客户端脚本

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

/// <param name="resourceName"></param>

public void RegisterClientScriptResource(Control control, Type type, string resourceName)

{

if (this.IsMsAjax)

{

if (RegisterClientScriptResourceMethod == null)

RegisterClientScriptResourceMethod = scriptManagerType.GetMethod("RegisterClientScriptResource");

RegisterClientScriptResourceMethod.Invoke(null, new object[3] { control, type, resourceName });

}

else

this.clientScript.RegisterClientScriptResource(type,resourceName);

}

public string GetWebResourceUrl(Control control, Type type, string resourceName)

{

//if (this.IsMsAjax)

//{

// if (GetWebResourceUrlMethod == null)

// GetWebResourceUrlMethod = scriptManagerType.GetMethod("GetScriptResourceUrl");

// return GetWebResourceUrlMethod.Invoke(null, new object[2] { resourceName, control.GetType().Assembly }) as string;

//}

//else

return this.clientScript.GetWebResourceUrl(type, resourceName);

}

}

The code basically checks to see whether the MS Ajax assembly can be accessed as a type and if so assumes MS Ajax is installed. This is not quite optimal – it’d be better to know whether a ScriptManager is actually being used on the current page, but without scanning through all controls (slow) I can’t see a way of doing that easily.

The control caches each of the MethodInfo structures to defer some of the overhead in making the Reflection calls to the ScriptManager methods. I don’t think that Reflection here is going to cause much worry about overhead unless you have a LOT of calls to these methods (I suppose it’s possible if you have lots of resources – think of a control like FreeTextBox for example). Even then the Reflection overhead is probably not worth worrying about.

To use this class all calls to ClientScript get replaced with call this class instead. So somewhere during initialization of the control I add:

protected override void OnInit(EventArgs e)

{

this.ClientScriptProxy = ClientScriptProxy.Current;

base.OnInit(e);

}

And then to use it:

this.ClientScriptProxy.RegisterClientScriptInclude(this,this.GetType(),

ControlResources.SCRIPTLIBRARY_SCRIPT_RESOURCE,

this.ResolveUrl(this.ScriptLocation));

Notice the first parameter is the control instance (typically this) just like the ScriptManager call, so there will be a slight change of parameters when changing over from ClientScript code.

Once I added this code to my controls the problems with UpdatePanel went away and it started rendering properly again even with the controls hosted inside of the UpdatePanels.

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!