[ZT] Best practices and common errors when adding a ScriptManager programmatically from a WebPart
2011-03-22 11:29
489 查看
Goal of this post This post aims at providing the most flexible and convenient way to make sure that a valid ScriptManager is available on page when you develop WebParts dealing with AJAX for ASP.net or Silverlight controls. Problematic overview ScriptManager is the key object making Silverlight and AJAX for ASP.net controls run on your ASP.net page. This non-visual control basically aims at loading JS scripts relevant for those technologies. Referring to MS documentation, it must appear as the first control in page controls tree in order to be available for any control requiring AJAX support. A ScriptManager can be embedded either from markup code of the hosting page, or programmatically during page lifecycle. So let’s imagine you are developing a WebPart embedding a Silverlight application. You want to redistribute this WebPart but you don’t want users to modify anything in their SharePoint portal to make it run. Basically, in your code, you will try to see if a ScriptManager is present on the page and if no you will add it on the fly. Then you have to face several issues: Where can I place my ScriptManager in the controls tree to make it available to relevant controls? Which event do I need to choose to attach my code? Will it be compliant with postback events, databinding cycles and WebParts connection management? Solution First, you need to add a new ScriptManager as the first control in the page form. Before adding anything, you have to ensure that no ScriptManager is already present on the page. if (Page != null && ScriptManager.GetCurrent(Page) == null) { Page.Form.Controls.AddAt(0, new ScriptManager()); }Then you need to determine where to place this code to be compliant with requirements evoked at question #3. The most reliable answer is to override the OnInit event of your WebPart and paste your code there protected override void OnInit(EventArgs e)An alternative approach could be to attach a handler to the page LoadComplete event, but it gives no gain to the previous solution. Common errors I’ve listed below some common mistakes and the corresponding errors when trying to answer to questions above. Do not add control directly to Page’s controls collection; add it to its Form property. If you try to attach a handler to the Page Init event, your code will never be executed because Web parts are added after the page InitComplete event.Page.Controls.AddAt(0, new ScriptManager()); Page.Form.Controls.AddAt(0, new ScriptManager());Striked code above will produce the following error Control 'ctl01' of type 'ScriptManager' must be placed inside a form tag with runat=serverDo not try to override the WebPart OnLoad event to place your code. If so, you will receive the error below The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.Do not try to add any handler to page events. Web parts lifecycle is too restrictive to use this approach. If you try to attach a handler to the Page Load or PreLoad events, your code will never be executed when your Web part is being added to the page and you will receive the error below (next attempts will work). The fact is that when a Web part is added to a page, any page event prior to LoadComplete is ignored. The control with ID 'XXX' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it. Script controls may not be registered before PreRender. |
相关文章推荐
- Best Practices for Paravirtualization Enhancements from Intel® Virtualization Technology: EPT and VT-d
- Best Practices and Commonly Made Mistakes When Using jQuery
- Real Web Project Management: Case Studies and Best Practices from the Trenches
- Oracle Weblogic nodemanager STOP script Bash and Python
- Best Practices for Managing Your Linux/Unix Performance and Availability @ JDJ
- Programmatically Adding to and Removing from a Person or Group Column
- Secure Coding Best Practices for Memory Allocation in C and C++
- Scalability Best Practices: Lessons from eBay
- 10 best practices from top coders at Google, Pinterest & more
- Secure Coding Best Practices for Memory Allocation in C and C++
- Postgres CopyManager and connection from Connection Pool
- GATK best practices对variation(only for SNPs and Indels)的鉴定以及对上游数据对处理
- Android Out of Memory Error Causes, Solution and Best practices
- Best and Worst Practices for Mock Objects
- 转:45 Useful JavaScript Tips, Tricks and Best Practices
- .NET Remoting Use Cases and Best Practices [翻译]
- No orientation specified, and the default is horizontal. This is a common source of bugs when childr
- Best Practices for Speeding Up Your Web Site (from Yahoo! Developer Network)
- Object-Oriented Design Knowledge: Principles, Heuristics And Best Practices
- r22 tools cause java.lang.VerifyError when building from ant script