Monday, February 21, 2011

ASP.NET 4.0- ScriptManager Enhancement Enable CDN Property.

ASP.NET 4.0 has been a great step forward to the programming. Microsoft has done incredible job with the performance. One of them is Enable CDN Property in asp.net 4.0 Script Manager. Let's explore it in details. As you all know that Microsoft is Providing Content Delivery Network for all the Ajax script that is used by Microsoft Ajax and All version of Jquery and JQuery UI. This Script manager enable cdn property will fetch all the script from that CDN Automatically. Developer don't have to worry about. As you know new generation browsers are like IE8 and Firefox are creating a new thread of each JS or CSS include in Webpage if they are coming from the CDN(Content Delivery Network). So this will be a great measure step towards it. Let's take an example to explore the things. I am taking one simple example which will demonstrate use of this. I am tasking a Textbox and Button. On the button click event it will update textbox's text property. I am going to use update panel to use Microsoft Ajax. For that first I need a script manager so I have taken script manager and and I have set its property EnalbleCdn=true. So without posting back whole it post back only that portion which are required. I have putted textbox in content template of update panel and I have created asynchronous post back trigger for button click event. So it will update the textbox text without creating post back. Let's how I have created. Following are Simple HTML for that.



<body>
   <form id="form1" runat="server">
   <div>
       
       <asp:ScriptManager ID="cdnScriptManager" EnableCdn="true" runat="server">
       </asp:ScriptManager
       
       <asp:UpdatePanel ID="cdnUpdatePanel" runat="server">
           <ContentTemplate>
                 <asp:TextBox ID="txtHelloWorld" runat="server"></asp:TextBox>
           </ContentTemplate>
           <Triggers>
                 <asp:AsyncPostBackTrigger ControlID="btnHelloWorld" EventName="Click" />
           </Triggers>
       </asp:UpdatePanel>
        
 
       <asp:Button ID="btnHelloWorld" runat="server" onclick="btnHelloWorld_Click"  Text="Hello World!!"/>
 
       
   </div>
   </form>
</body>

protected void btnHelloWorld_Click(object sender, EventArgs e)
{
    txtHelloWorld.Text = "Hello World";
}


This will be a second part of ASP.NET 4.0 Script Manager enhancement. In this post I am going to explain about AjaxFrameworkMode Property. In Earlier asp.net version of script manager it will load entire Microsoft Ajax library whether its required or not. In asp.net 4.0 script manager we are having AjaxFrameMode property where we can set mode as explicit and we add only js that are required or not.
There are three values of AjaxFrameworkMode properties supported in asp.net
  1. Enabled- Specified that ASP.NET 4.0 scriptmanager will automatically load MicrosoftAjax.js file which is core element of Microsoft Ajax library.
  2. Disabled- This Specified that Microsoft Ajax Script features are disabled and script manager will not have any reference to any javascript files.
  3. Explicit- Specifies that you will explicitly include script references to individual framework core script file that your page requires, and that you will include references to the dependencies that each script file requires.
So here is example if require only Microsoftcore.js then we can use this explicitly without loading other unnecessary files.


<asp:ScriptManager ID="myScirptManager" AjaxFrameworkMode="Explicit" runat="server">
 
<Scripts>
    <asp:ScriptReference Name="MicrosoftAjaxCore.js" />
        
</Scripts>
</asp:ScriptManager>


No comments :

Post a Comment