Clarity, Technology, and Solving Problems | PracticeThis.com
WP7 App with Key Windows Azure resources – Slides, Videos, How-To’s, and T-shooting – for quick consumption on the go.
How to dynamically populate the content of a control based on Web Service call triggered by another control? DynamicPopulate extender to the rescue:
DynamicPopulate is a simple extender that replaces the contents of a control with the result of a web service or page method call. The method call returns a string of HTML that is inserted as the children of the target element.
This post to summarize basic steps of using AJAX Control Toolkit's DynamicPopulate extender. Plus customer's case study of how it was implemented with ASP.NET Masterpage for performance and UX improvement.
Summary of steps
Following section describes each step in details.
[System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public static string GetHtml(string contextKey) { // A little pause to mimic a latent call // This is the place to perform server side // code like DB access System.Threading.Thread.Sleep(350); return String.Format("Persona: {0}", contextKey); }
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit"%> Add AJAX Script Manager to the page: <asp:ScriptManager ID="ScriptManager1"runat="server"/>
Add DynamicPopulateExtender control to the page:
<ajaxToolkit:DynamicPopulateExtender ID="dp"BehaviorID="dp1"runat="server" TargetControlID="form1$Label1" ClearContentsDuringUpdate="true" ServiceMethod="GetHtml" />
Notice TargetControlID is set to "form1$Label1". This is the Label control to be updated with the Html string returned by the server side code I described in Step 2. $ notation means nesting - read "Label1 control inside form1 control". It can be any nesting depth. Save your work and then view in browser to make sure that no exceptions generated.
<script type="text/javascript"> function updateDateKey(value) { var behavior = $find('dp1'); if(behavior) { behavior.populate(value); } } Sys.Application.add_load(function(){updateDateKey('Alik Levin');}); </script>
Notice initialization function call - the last row. It invokes the client side function to call into server side for the first time when the page is rendered into the browser for the first time.
<input type="radio" name="rbFormat" id="r0" value='alik' onclick="updateDateKey(this.value);" checked="checked" />click to set 'alik'<br/> <input type="radio" name="rbFormat" id="Radio1" onclick="updateDateKey(this.value);" value='levin' />click to set 'levin'
I was asked by a customer to offer a solution for very responsive UX [User Experience] while avoiding annoying refresh of the web page. Natural answer was AJAX. The customer also asked to provide the solution for common look and feel. Master pages was my answer. Then another ask came through - content page must update the sidebar that is part of the Master page.... hmm A-ha! Use DynamicPopulateExtender.
My Related posts
AJAX Security - Client Side Validation Is For Usability Only, Not For Security
ASP.NET 3.5 Extensions: Basic Steps To Create Dynamic Data Web Application - Focus On Security and Performance
Sample VS2008 project that demonstrates DynamicPopulateExtender can be found on my SkyDrive:
Watch UX [User Experience] in the video below:
Enjoy.
This post to describe basic steps to write HttpModule and how it rescued mission critical application from not hitting the dead line.
HttpModule is the mechanism that facilitates implementing cross cutting logic for incoming ASP.NET requests. ASP.NET uses it extensively under the covers for its needs too - Session management, Authentication to mention a few. What I love most with HttpModules is that there is no need to alter the application itself - just implement IHttpModule interface and tweak web.config. That is all.
Steps to build HttpModule
Next section describes each step in detail.
Here I implement any logic I want to run before the page got hit. HttpModule serves as a filter or gatekeeper for incoming Http requests. Following case study explains how such approach saved mission critical application.
Case study
Critical mission application was to go live in few days. The schedule was tight. All functional and integration tests went well. The last one was load and stress tests. During the the load test turned out that one of the network components was crashing as a result of memory leak. The component was sending http headers. We decided to configure the component to send the data in Query Strings instead Http Headers [making story short...]. We needed to find the way to teach the application to look the data in Query Strings instead Http Headers. HttpModule to the rescue!! We developed simple HttpModule that was hijacking the request, scrapping the Query String and setting the data inside Http Headers, where the application was looking for it. Heaven. Worked like magic.
Caveats
Adding Http Headers in .Net is not straight forward since the collection is read only. Reflection to the rescue! Following code explains how to hack Http Headers collection to make it writable - http://forums.asp.net/p/979853/1250479.aspx#1250479. Then we needed to drop the extra Query String before it got to the application. My search landed directly to ScottGu's RewriteUrl post - http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx. What's left is make reflection run faster by caching type's constructor as described here - http://weblogs.asp.net/bradygaster/archive/2003/11/26/39952.aspx
My Related posts:
Sample VS2008 project can be found on my SkyDrive:
This post walks through the steps I've taken to create simple Dynamic Data Web Application. I just loved the development model for DTO [Data Transfer Object] and Input Validation options.
Following are detailed explanations for each step.
Validation rules propagated to both client [for usability] and server [for security], this is how violation of input validation looks in it default view:
Heaven.
Focus on Security
I can create data driven web pages using GridView and DataSets. The drawback is that validation is not that straightforward. On other hand I can create custom collections and manually data bind it - the code is much nicer and cleaner and validation rules are easy to add but there is the need of writing extra code. In the case of Dynamic Data there is fantastic combination of design time productivity and also clean code where validation rules are applied directly to the model. Less room for mistake to introduce security vulnerability.
Focus on Performance
Ready to go templates that are generated with the default Dynamic Data projects already implement AJAX and paging. It reduces dramatically amount of data that round trips over the wire. Large HTML output - including ViewState - is one of the biggest performance vulnerabilities I've noticed recently. AJAX and paging is a great way to overcome this issue.
My related posts
Related materials