Archive

Archive for February, 2009

ASP.NET Advanced Grid View

February 18, 2009 Leave a comment

ASP.NET’s GridView is great, but admittedly there are limitations to what it can do.

Some nice features which are missing include

  1. Advanced Search
  2. Print
  3. Save

I’ve written an advanced gridview class which is able to perform all these

GridView Design Mode

This gridview comes with the ability to

  • Display Create button which goes to another url when clicked
  • Save button to save all the records in the Gridview (yes all the records)
  • Print button to print all the records in the GridView (same as save button, but will pop up a new window to do the print)
  • Advanced Search which gives you “google like” search e.g using “+” , “-“, quotation marks to mark words as one key term, OR. e.g aaa bbb “”ccc ddd”” +eee -fff OR ggg

Custom properties defined in this Gridview allow you to customize the top search portion

GridView Additional Properties

Click here to download the class and use it in your code

Categories: ASP.NET, Uncategorized

Performance Programming with ASP.NET C#

February 16, 2009 Leave a comment

It is easy to develop using ASP.NET, you can have a web application with database operations up in less than half an hour. But this ease of development comes at the expense of several performance hits.

  1. Viewstate
    Web sites are stateless by design, which means that anything you have on the page is not retained when you submit the data. What ASP.NET tries to do is to save the state on the page so that state is retained when the page submits its data.

    However this quickly becomes unmanageable when you have too many controls on the page, especially if you have large server controls e.g gridviews. As viewstate is stored on the page itself, a simple page with a few form elements may end up sending large amounts of data on postback, the majority of which is the viewstate itself.

    Other than increasing page size, there is also the performance cost incurred when serializing and deserializing the viewstate.

    Solution:

    • if a page is an output page and does not do any further processing, you can disable the viewstate for the page <% Page EnableViewState=”false”.
    • If the control on the page does not handle any events, is not a data-bound control or the data i constantly refreshed on postback, then you can disable the viewstate for the control
    • If all else fails, you can look into storing viewstate on the session state using SessionPageStatePersister (There is a drawback because the session state may expire before the user is done with what he or she is doing on the page – think blogs, wikis etc)
  2. String concatenation & comparisonHaving experience with int i = 0; i = 1 +2, you decide to go ahead and do the same for strings. string s = “aa” + “bb”This is very bad programing, simply because strings work differently from numeric primitive data types. When you do this type of string concatenation, quite a fair bit of resources are created to hold the strings and results

    The same can be said for case insensitive string comparison, usually you would do something like string1.toLower() == string2.toLower(). Same thing occurs, temporary space needs to be created to store the lowered strings and then do a compare.

    Solution:

    use stringbuilder for string concatenation functions
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(“aa”);
    sb.Append(“bb”);
    sb.ToString();

    use the overloaded string compare function if you want to do case insensitive searches
    String.Compare (string strA, string strB, bool ignoreCase);

  3. For loops with easily checked conditions perform far better than for each loops. However if the for loop condition requires a property call (e.g list.count()) then it will perform much slower, in this case get the count before the loop and assign it to a variable and in the for loop check against this variable.
  4. If you have multiple exit conditions, put them when it is first possible to check and also put the one most likely to happen right on top. This reduces the amount of checks needed to exit, which can give you a performance improvement.
Categories: ASP.NET, Performance

Connecting to Windows Live

February 15, 2009 15 comments

There are not many posts on how to connect to Windows Live, this guide attempts to help users with this connection

Requirements

  1. Have a valid domain name which you can manage
  2. Register a Windows Live application here
  3. Download code sample from here
  4. Update the following parameters inside web.config, taking the values from the Windows Azure summary page for the application
    <add key=”wll_appid” value=”[your appid]”/>
    <add key=”wll_secret” value=”[your secret key]”/>
    <add key=”wll_securityalgorithm” value=”wsignin1.0″/>
    <add key=”wll_returnurl” value=”[your return url]” />
    <add key=”wll_policyurl” value=”[url to your policy file]” />
  5. Browse to the domain to test (working sample can be found here)

Code run thru

policy.html

  • this is the policy file which is required by windows life

wlauth-handler.aspx

  • This page handles the ‘login’, ‘logout’, ‘clearcookie’ and ‘delauth’ Web Authentication and Delegated Authentication actions. When you create a Windows Live application, you must specify the URL of this handler page.

default.aspx

  • this is where all the main action takes place
  • when you first access this page, you will see a sign in link, click on it to begin the sign in process to Windows Live.
  • Thereafter, Windows Live will redirect you to wlauth-handler.aspx which will then handle the login cookie, and redirect you back to default.aspx
  • Now you will see something likeNow this application knows that you are the user with ID = “xxxxx”. Please click here to grant consent so that we may access your Windows Live data
  • In order for the app to access your Live Data, you need to click on the “click here” link
  • Once that is done, you will see “We have found a consent token associated with your ID.”
  • Click on Show Me my Contacts for the app to query Windows Live for your contacts and display it out
Categories: ASP.NET

Performance Programming with ASP.NET Ajax

February 9, 2009 Leave a comment

At first glance, ajax seems to be a cinch with asp.net 3.5, what with all the updatepanel, timers and such. However behind this ease of development hides a danger which most developers may not notice.

Although updatepanels and timers are easy to use, each update triggers a complete postback and if your viewstate is on the page, its an extremely large form post. On the server side, each event of a normal postback is called, and the whole control tree is generated to handle the ajax form post. This is quite intensive both for the server and for the client.

This can be good or bad depending on how you look at it. Its good because it ensures that the postback is a valid one, and also gives you flexibility on the controls you want to change and to include in any javascript that you need, all these being done on the code behind, without the need to fiddle with all the client side javascript required to perform these actions.

On the flip side, there are also client side apis and scriptmanager service references which gives you complete flexibility to go ahead and program using client side javascript.

Nexus initially went with the first approach (i.e a whole bunch of updatepanels), but the results were less than impressive. Moving onto the second approach required a revamp of the whole source code, but this gave the user a much better experience along with substantially reducing server load.

To reduce the footprint on the server side, i utilized ScriptManager’s ServiceReference parameters

<asp:ScriptManager ID="ScriptManager" runat="server"
      onasyncpostbackerror="ScriptManager_AsyncPostBackError">
<Services>
    <asp:ServiceReference InlineScript="true" Path="JSON.asmx" />
    <asp:ServiceReference InlineScript="true" Path="ProxyAsync.asmx" />
</Services>
</asp:ScriptManager>

ServiceReferences exposes the webservice functions in client side javascript so you can call these webservices without having to write complicated javascript. for example, i have a webservice function called UserFirstLoad(). This function is called the everytime the user logs into Nexus.

[WebMethod]
[ScriptMethod(UseHttpGet = false, XmlSerializeString = true)]
public JSON_Tabs UserFirstLoad()

Instead of writing javascript to interface to this webservice, ScriptManager automatically writes it for you and exposes it nicely as JSON.UserFirstLoad(), a function which i can easily call and have the capability to do error handling if need be.

As i’m not using UpdatePanel, GUI updates are done directly via DOM. Fortunately Ajax ClientScripts provide some global functions which i can use to

  1. Locate the element i want to update ($get)
  2. Add events to the element ($addHandler)

It is also fortunate that Visual Studio 2008 comes with javascript intellisense, which autocompletes your javascript as you type.

As Javascript is OOP, i decided to build my client script scripts around objects and functions. This gives me the flexibility to overwrite functions and have nice clean code which i can then easily debug with VS2008.

Categories: AJAX, ASP.NET, Performance