Archive for March, 2008

Since the Microsoft Ajax was released, a lot of ASP.Net developers began using UpdatePanel for streamlining their applications and indeed the WWW starts to seem a little bit more fluent nowadays.

Unfortunately UpdatePanel is not used always in the right places or in the right way, some applications begin to feel clumsy and slow and you’re thinking WHY?! Below I compiled a small list of tips and tricks on how and where to use an UpdatePanel.

  • In web.config use this:

<system.web.extensions>
    <scripting>
        <scriptResourceHandler enableCompression=“true” enableCaching=“true”/>
    </scripting>
</system.web.extensions>
This will improve script handling by adding compression and caching

  • When deploying to a live server use: <compilation debug=“false”> 
  • Don’t use AJAX to update the complete page by putting everything in a UpdatePanel. You want to save time and raffic when running the web page. Never update parts of the web site that can be changed using JavaScript and DHTML (DOM).
  • Have in mind that there are a couple of visitors that have JavaScript disabled or using a web browser with an older or less JavaScript implementation like the most mobile devices have. What does your visitor see if everything is disabled? I don’t recommend to have the full web site available as a JavaScript disabled version!
  • Cache the same requests on client-side web browser or implement any caching on the web server.
  • Remember that the page lifecycle is the same also during an asyncronous postback. That means that the Page_Load for example is executed also during an asyncronous postback. The IsPostback will be true in an asyncronous postback also.
  • Set the UpdateMode to “conditional” in the update panel

There are more,  feel free to add your own

Comments 1 Comment »

Hi,

Yesterday I was asked a simple question about using a separate browser window for selecting a value and then using the selected value in the main browser window. I’ve done this before but I realized that for a novice web programmer it is not trivial especially when you are accustomed to the ASP.Net programming model.

Let’s consider the following scenario. You are editing an order for buying some books. You enter your name, address, and other information in the textboxes, but you are then required to select the book you are searching for. The problem the programmer is facing is that on one hand he has alot of books in the database (let’s say 5000 items), which means that it would be overkill to fill a dropdown with those names and hard for the user to select from it (too many options), on the other hand ussually he does not have the space to put a complex search in place. So the most ideal situation is to put a separate window that opens on command and where the user can use a more complex algorithm to search for the book he wants. Ok, this is not a very good example as in real world you first select the book, add it to cart and then go to the checkout and fill all remaining data. Still let’s say this is ok for the sake of the demonstration.

So, we have a main window and from there we must open a second window where the user selects some data, then the selected data must be passed back to the main window. Well, ASP.Net does not offer something out of the box for this as the Asp model is based on a single window. So what you can do with asp is to redirect the page to a search window and then upon selecting the desired value redirect it back to the main window and put the values there (you can pass the values either through the Session object or QueryString). This unfortunately requires some extra programming and also might be a little bit difficult for the user to understand.

Thus we get the the javascript sollution. User clicks on a button, a new browser window opens, user makes selection, the new browser window closes and the selection is put in the main window. Below you can find an example and some explanations.

This is the main window:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”master.aspx.cs” Inherits=”_master” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Master window</title>
   <script language=”javascript” type=”text/javascript”>
      function openChildWindow(tagetControlId) 
      {
         var s= “child.aspx?id=”+tagetControlId;
         window.open(s,“”,“width=400,height=200,status=no,toolbar=yes,menubar=no,location=no”);
         return false;
      }
   </script>

</head>
<body>
   <form id=”form1″ runat=”server”>
      <div>
         <asp:TextBox ID=”txtResult” runat=”server”></asp:TextBox>

         <asp:Button ID=”btnOpen” runat=”server” Text=”Open child” OnClientClick=”openChildWindow(‘txtResult’)” />
      </div>
   </form>

</body>
</html>

This is the child window:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”child.aspx.cs” Inherits=”child” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >

<head runat=”server”>
   <title>Child window</title>
   <script language=”javascript” type=”text/javascript”>
       function Done()

      {
          p = window.opener.document; //parent window
          target = p.getElementById(GetElementFromQuery(location.search.substring(1),“id”));

          target.value = document.getElementById(“txt1″).value+“|”+document.getElementById(“txt2″).value;
          window.close();
       }
       function GetElementFromQuery(url, name)

       {
         x = url.split(“&”);for (i=0;i<x.length;i++)

         {
             if (x[i].split(“=”)[0] == name)

             {
                return x[i].split(“=”)[1]

             }
          }
          return “”;

        }
   </script>

</head>
<body>

   <form id=”form1″ runat=”server”>
      <div>
         <asp:TextBox ID=”txt1″ runat=”server”></asp:TextBox>
         <asp:TextBox ID=”txt2″ runat=”server”></asp:TextBox>
         <asp:Button ID=”Select” runat=”server” OnClientClick=”Done()” Text=”Close”/>
      </div>
   </form>

</body>
</html>


So, let’s summarize. In the main window, which I called master, you have a textbox where we want to put the results and a button which when pressed we want to open the child window. Observe that we use the OnClientClick instead of OnClick since we will be calling a javascript function and there is no need to go to the server (Reminder: OnClientClick is fired before OnClick and in the javascript code of the OnClientClick if we return false, the OnClick will not be fired anymore).

The javascript function called on the click event is “openChildWindow” which has as parameter the id of the control that will receive the selection made in the child window. In the method we construct the URL of the child window where we will pass the id as a query string, then we use the window.open to open the child in a separate browser window.

In the child window we have 2 textboxes from where we will take the selection. When the user presses the button, the “Done” function will first retrieve the opener object, which is a handle to the window that oppened the child window (this is normally null, unless the window was opened as a result of a window.open call). then we retrieve the html object from the opener window that has as id the string passed as query string. we use a separate function for that, which will search within the query string the keyvaluepair with the specified key and retrieve the value (we used “id” as we sent it like this from the master window). Afterwards we set the value of the target element with the values from the two textboxes and close the window.

This is highly extensible as the child window can host a normal ASPX page and the results can be multiple and returned in various locations within the master page (just use more keys in the QueryString).

That’s it for now. Feel free to adapt the above code to your needs and post any questions you might have.

Edit: You can find also a more detailed example here

Comments 3 Comments »

Hi there,

I just read a small article on slashdot.org about Intel being on the verge to release 80 and 120Gb Flash drives (solid state drives). Beside this being a quite astonishing news, because we use to think Intel is making only processors and chipsets, one gas got me thinkinh of the current state of computing within the PC area.

I got now a rig with 4Gb Ram, and a SATA2 WD400Gb hardrive and still I don’t feel like my computer is flying or running extremly fast, although there are improvements. But this has a very solid reason – the harddrive. This is the slowest component in a system. The ram supports nowadays around 6400Mb/sec when using 800Mhz memory or 4267Mb/sec when using 533Mhz memory and the internal bus of a modern chipset can handle that easily; and dual core / quad core processor can process most available data on the bus. But all this hit a wall when communicating with the harddrive. Although the SATA2 interface can handle a theoretical 3000Mb/sec, and the best deliver around 2000Mb/sec, because of the physical constraints and how the drives are build, a typical read/write operation is around 1000Mb/sec and that is in ideal conditions.

So what can be done to improve this state of matters. You really have few options and all of them are expensive.

  • Buy a 10.000 rpm drive. This is a simple sollution, but an expensive one as a 32Gb drive has the same price tag as a 500Gb 7.200Rpm drive.
  • Get 2 smaller drives, let’s say 2x120Gb 7200rpm drives and put them in Raid 1 if your motherboard supports that. This can theoretically deliver around twice the bandwidth of a single drive both in writing and reading. This I think is the cheapest sollution at the moment because they can deliver more than a 10.000rpm drive in terms of bandwidth at a price twice as low.
  • Get a SCSI harddrive. Well I am no expert in this but I know you get more performance when using it right but yes, it’s expensive. You have to have SCSI harddrives and also a SCSI controller which are not over the top in pricing but it will set you back quite some.
  • Use solid state drives. Ugh, this is still a dream for the average user. SSD can deliver 0.1ms seek time and top models can sustain continuous 1000Mb/sec read operations while a normal mechanical drive has fluctuations of this value. Reading small files scattered around the disk is also faster that mechanical drives. Also there is small power consumption, better resistance to physical shocks, etc. Still there are downsides at the moment:
    • write operations are below the rate of a mechanical drive
    • price tag is simply prohibitive; a good SSD with 64Gb will set you back with 800-1000USD.
    • technology is still in it’s infancy. Many things can change and if manufacturers keep their promise, in two months you may realize your top of the line product is now rated middle segment.
  • In very specific scenario the HDD replacement may be an overkill because there are better suited sollutions. In my case, a web developer, I saw cases where I could increase productivity a lot even if I could manage to move 1Gb of data from HDD to a more faster storage device. For example in RAM. Yes, why not? A good development machine has 4Gb of Ram. Windows, even Vista stays below the 1.5Gb mark so you have 2.5Gb of Ram that can be put to good use. So you can emulate a partition in Ram. There are tools for that and Microsoft even delivers a free one. Just move your projects there and set the database tempdb to be stored on that partition and voila, you have them on a 300 times faster drive.

So, there is a problem and there are sollutions. The choice depends really on the person, scenario and budget. At this point I would recoment 2 drives in Raid1 if you need extra performance but that’s just my pick, you have to find yours.

If you have better ideas, let us know …

Update

I just found a news on TomsHardware (here) about OCZ anouncing a SSD with SATA2 capable of sustained 120Mbps reads and 100Mbps writes which is almost twice as fast as the old SSD generation and it’s available already in the UK with prices starting around 700USD for the 32Gb model and 1300USD for the 64Gb model.

Comments No Comments »

There are quite a lot of us who have a lot of papers on their desks with notes, tasks, observations. And there are those who use notepad alot to store usefull information on the project. Still it’s not easy to do this organization tasks when programming as it makes you either leave the development IDE or take your hands of the mouse and keyboard.

Why don’t we have a way to make quick notes within Visual Studio 2005. Well we actually do, although I have not seen any using it. Just go to the View menu and select Task List. A dockable window will come in front and you can dock it somewhere, I dock it in a group of windows below the code, so that it is easy accessible. How do you use it, well, just double click on the first available row in the list and enter your comments/tasks there.

For example, you have a task which is actually containing 7 inner tasks/functionalities. At some point you have to hardcode a variable so that you can go faster to another task and at some point you have to get back and correct this. This happens. No, no, don’t shake your head! you did it at some point. The problem is that you will likely forget this. Well, add a task like “Correct the hardcoding on file myfile.cs line 44″. At some point you will check that list and you can see what you still need to do and well … do it. Then just mark the task as completed (checkbox on the same line left side), or delete it.

No more yellow stickers and endless blocknotes :)

Cheers

Comments No Comments »

Well, URL Redirect, which is mostly used in PHP for making user friendly urls is supported by IIS but with a twist, at least in IIS7.

I banged my head for a couple of hours and for some reason I could not make it work. Lastly I decided to google for url rewrite AND IIS7. To my surprise there is an additional step to be done when registering your HttpModule (the one that will handle the url rewriting). You not only need to add it to the modules tag of the web.config, but also to the <system.webServer> modules tag. See example below (of course this is a stripped down code, you have to intertwine it with your existing web.config)

<system.web>
    <httpModules>
        <add name=UrlRewriter type=Rewrite, UrlRewriter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null />
    </httpModules>

</system.web>
<system.webServer>
    <validation validateIntegratedModeConfiguration=false />
    <modules>
        <add name=Rewriter type=UrlRewriter.Rewrite preCondition=managedHandler />

    </modules>
</system.webServer>

Lots of thanks to Scott Guthrie for his article on url rewriting in asp.net: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

and also to Denis van der Stelt for his observations on IIS7 and url rewriting: http://bloggingabout.net/blogs/dennis/archive/2006/11/29/IIS7-and-Url-Rewriting.aspx 

07-03-2008
Yesterday I had to upload to a hoster a website using the above technique and it would not work. The hosting was using IIS6, so the ASP.Net would look at <system.web><httpModules> for additional modules, whereas IIS7 would not take into account that, but the settings in <system.webServer>. So what was wrong? The application complained that it could not load the type Rewrite. So I had a closer look and … I wrote the declaration wrong :( . Below you will find the correct add declaration to be added in httpModules:

<add name=UrlRewriter type=UrlRewriter.Rewrite, UrlRewriter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null />

Comments 2 Comments »