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

2008-02-22 09:41:18来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Over the last few weeks since MS Ajax Beta rolled around I’ve been getting a number of reports of the wwHoverPanel control running into some problems when running in combination with MS Ajax. The controls themselves don’t interfere with MS AJAX directly, but if you’re sticking the controls inside of an AJAX UpdatePanel() there’s a problem as the script code that the controls spit out don’t get properly generated into the callback generated updates. With the script code missing the controls still work but exhibit some unexpected behaviors. For example a hover panel placed into an update panel will lose it’s positioning in many cases and instead of popping up at the current mouse cursor position will pop up at the border of the container control it lives in.

The problem is that Microosft decided in MS AJAX Beta to go with a completely separate script generation engine which is driven through the ScriptManager control. The MS Ajax ScriptManager mimics many of the ClientScript object’s methods, but provides them as static methods (thankfully! without that we’d be really screwed).

So methods like RegisterClientScriptBlock, ResgisterClientScriptResources – anything that deals with getting script code into the page have related static methods in ScriptManager. The ScriptManager methods pass in the Control as an additional first parameter but otherwise mimic the existing ClientScriptManager.

This new behavior puts existing controls into a bind though – if code uses ClientScriptManager then UpdatePanels will not be able to see the script code (if it needs updating in a callback). But at the same time the control developer can’t make the assumption that the MS Ajax ScriptManager actually exists.

The end result of all of this is that it’s not exactly straight forward to deal with this mismatch and what needs to happen is that a wrapper object needs to be created that can decide which control to use. The wrapper needs to deal with deciding whether MS Ajax is available in the application and if it is, using Reflection to access the ScriptManager to write out any script code.

I can’t take credit for this though: Eilon Lipton posted about this issue a while back and his code really was what I needed to get this off the ground, I just wrapped the thing up into a ClientScriptProxy object that I used on a handful of controls. I basically added a handful of the ClientScript methods that I use in my applications. Here’s the class:

[*** code updated: 12/12/2006 from comments *** ]

/// <summary>

/// This is a proxy object for the Page.ClientScript and MS Ajax ScriptManager

/// object that can operate when MS Ajax is not present. Because MS Ajax

/// may not be available accessing the methods directly is not possible

/// and we are required to indirectly reference client script methods through

/// this class.

///

/// This class should be invoked at the Control's start up and be used

/// to replace all calls Page.ClientScript. Scriptmanager calls are made

/// through Reflection

/// </summary>

public class ClientScriptProxy

{

private static Type scriptManagerType = null;

// *** Register proxied methods of ScriptManager

private static MethodInfo RegisterClientScriptBlockMethod;

private static MethodInfo RegisterStartupScriptMethod;

private static MethodInfo RegisterClientScriptIncludeMethod;

private static MethodInfo RegisterClientScriptResourceMethod;

//private static MethodInfo RegisterPostBackControlMethod;

//private static MethodInfo GetWebResourceUrlMethod;

ClientScriptManager clientScript;

/// <summary>

/// Determines if MsAjax is available in this Web application

/// </summary>

public bool IsMsAjax

{

get

{

if (scriptManagerType == null)

CheckForMsAjax();

return _IsMsAjax;

}

}

private static bool _IsMsAjax = false;

public bool IsMsAjaxOnPage

{

get

{

return _IsMsAjaxOnPage;

}

}

private bool _IsMsAjaxOnPage = false;

/// <summary>

/// Current instance of this class which should always be used to

/// access this object. There are no public constructors to

/// ensure the reference is used as a Singleton.

/// </summary>

public static ClientScriptProxy Current

{

get

{

return

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在VS2003 IE7.0下调试asp.net权限问题的解决办法

下一篇:MSBuild, NAnt, NUnit, MSTest所带来的不爽