ThreadAbortException

November 2008 Entries

Virtual TechDays November 2008

Virtual TechDays is back and this time, I would be covering topics around ASP.NET AJAX Improvements in .NET 3.5 Service Pack 1, Internet Explorer 8 for Web Developers and .NET Application Development.

If you want to have a quick preview of what I am talking on, you can check my previous posts  

However, there are a few more enhancements that help AJAX driven web developement and if you are an ASP.NET / AJAX Developer you really dont want to miss the sessions.

The ASP.NET and IE 8 Sessions are on Day 1, and the .NET Development session is on Day 2.

I am also delivering a Chalk Talk on ASP.NET MVC on Day 2.  This would be the first of its kind over Virtual TechDays and I hope this format works out for all you folks who dont want to see a presentation and basic sample (well in certain cases even basic-advanced samples)

 

To register, please visit http://www.virtualtechdays.com/

Cheers !!!

 

 

 

<microsoft:ScriptReferenceProfiler> for ScriptCombining, available at CodePlex, ASP.NET AJAX

If you have seen Bertrand’s video on Script Combining to improve AJAX performance, you must have really felt like using it in your site to figure out the scripts downloaded in your page and minimizing the size, improving performance etc.,

Well, this post is just to say that, the ScriptReferenceProfiler is available at CodePlex http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=13356

You need to download the binaries and add it as a reference to your website/project.  Once you add the reference, you can add the page directive declaration as below:-

%@ Register Assembly="ScriptReferenceProfiler" Namespace="ScriptReferenceProfiler" TagPrefix="cc1" %>

and then start using it in your page as below:-

<cc1:ScriptReferenceProfiler ID="ScriptReferenceProfiler1" runat="server" />

Script Combining is one of enhancements to ASP.NET AJAX shipped as a part of .NET 3.5 SP1.  The actual process of script combining is when you identify your scripts and put them under <ComositeScript> collection under ScriptManager.  The ScriptRefereceProfiler is just an utility (great utility by the way!) to identify your scripts.

Therefore, you can use the feature today in your websites and minimize the script downloads and enhance the performance. 

Cheers !!!

Enabling the Browser Back Button for GridView, ASP.NET AJAX History

Updated the code Snippet with the Method and Event Handler parameters for the ScriptManager's Navigate Event.

One of the common navigation tools in a website is the back button of the browser.  People use the back button quite frequently to go back to the page already visited.   When there is a postback, the browser is updated with the information of the page visited and hence the back button gets enabled automatically.   Assuming there is a GridView control that we are using in a page which fetches a lot of records, one of the common practices is to enable paging for the GridView such that the initial set of records loads quicker and people can page through the next set of records on a requirement basis.

When this GridView is in a normal ASP.NET Page, when users click on the paging buttons or links, the browser history gets updated and hence, if the users want to check the previous set of records, they can click on the back button and do the same.

However, in case of an ASP.NET AJAX enabled page, typically the GridView inside an Update Panel, it becomes impossible since the postback happens asynchronously and the browser history isn’t aware of the postback and hence the back button isn’t enabled.

To overcome this, ASP.NET 3.5 SP1 provides the History control that allows you to add history points to your AJAX Enabled page’s post backs and hence enable back button navigation for the users.

Let us examine this with a sample on Grid View.  First, let us add a regular GridView control to an ASP.NET Page and configure it to use the NorthWind Database’s “Alphabetical list of products” table.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
        DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
          <asp:BoundField DataField="ProductName" HeaderText="ProductName"
            SortExpression="ProductName" />
          <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
            SortExpression="QuantityPerUnit" />
          <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
            SortExpression="UnitPrice" />
          <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
            SortExpression="UnitsInStock" />
          <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
            SortExpression="UnitsOnOrder" />
          <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
            SortExpression="CategoryName" />
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
      </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [CategoryName] FROM [Alphabetical list of products]">
      </asp:SqlDataSource>

Note this sample assumes you have the Northwind Database installed and you have the connection string configured in the web.config to connect to the database.  You can download the same from http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034&displaylang=en for SQL Server 2000, 2005 etc.,

When you run this page, you will be able to page through the records and hit back button to go to the previous set of records.

Let us add Script Manager and Update Panel to this page and then move the contents into the Content Template of the Update Panel.

<asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>

>> The above GridView code snippet goes here


     </ContentTemplate>
     </asp:UpdatePanel>

Now when you run the page and click on the paging numbers i.e. 1,2,3..you will notice that the back button isn't enabled since this has been an asynchronous postback within the update panel.

To handle this, we need to do the following things

1. Set the EnableHistory=”true” for the ScriptManager (in this case, ScriptManager1).  The updated ScriptManager declaration looks like this

<asp:ScriptManager ID="ScriptManager1" runat="server" EnableHistory="true">
      </asp:ScriptManager>

2. Handle the OnPageIndexChanging event of the GridView with an event ex.- OnPageIndexChanging="GridView1_PageIndexChanging" needs to go in the GridView declaration above.

3.  Add the handler code in the code behind as follows:-

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {

        if (ScriptManager1.IsInAsyncPostBack && !ScriptManager1.IsNavigating) {
            ScriptManager1.AddHistoryPoint("Index", GridView1.PageIndex.ToString());
        }
    }

Basically we are checking if the ScriptManager is in an asynchronous postback and the ScriptManager is not currently navigating.  The AddHistoryPoint is the method that helps us to define history points and we provide the PageIndex to the method using the GridView’s pageindex property.

4. Handle the Script Manager’s Navigate event in the declaration as OnNavigate="ScriptManager1_Navigate"

5.  Add the Handler code in the codebehind as follows:-

 protected void ScriptManager1_Navigate(object sender, HistoryEventArgs e)
    {
        string indexString = e.State["index"];
        if (string.IsNullOrEmpty(indexString))
        {
            GridView1.PageIndex = 0;

        }
        else
        {
            int Index = Convert.ToInt32(indexString);
            GridView1.PageIndex = Index;
        }

Once we do the above, when you run the page and navigate across the pages, you could see that the back button is enabled and we can navigate using the back button.

Note that, if there are multiple GridView controls within the updatepanel, we need to handle them separately as currently, we are only handing GridView1.  However, if the GridView or any other control is outside the scope of Update Panel, then back button enablement is taken care automatically.

Also, Sorting needs to be handled as well.  For a sample on Sorting check http://aspnet.4guysfromrolla.com/articles/100808-1.aspx

Cheers !!!

ASP.NET Team Blogs

At PDC this year, I had a chance to meet most of the product team members from ASP.NET and Visual Studio for Web Development.  The good news is that each one of them have their individual blog as well as a team blog where they share tons of good information.  That is the best news you can get if you are eager to hear on ASP.NET.  So here below is the list of informative blogs which are pretty active, precise and more importantly straight from the people who are creating it

Scott Guthrie   http://weblogs.asp.net/scottgu  

Brad Abrams  http://blogs.msdn.com/brada/  

Scott Hunter  http://blogs.msdn.com/scothu/  

Scott Hanselman http://www.hanselman.com/blog/   

Phil Haack http://www.haacked.com  

Bertrand Le Roy http://weblogs.asp.net/bleroy/   

Eilon Lipton http://weblogs.asp.net/leftslipper/   

Stephen Walther http://weblogs.asp.net/StephenWalther/  

Visual Web Developer Team Blog http://blogs.msdn.com/webdevtools  

Mikhail Arkhipov http://blogs.msdn.com/mikhailarkhipov/  


There are more blogs that you can find linked to the above blogs as well.  So go ahead subscribe and happy coding with ASP.NET.

Cheers !!!