Quick wins on performance in your web applications

16 09 2011

According site requirements and business needs, your web application has to reach sometimes a higher level of reactivity. Most of the business applications does not encounter this situation because the main focus is set to business rules and engines(data manipulations and processes) and not really about interfaces and network… However, it’s an other deal with public and mobile web sites.
There are already many sites which propose some tips on site optimizations, but I’ll focus on ASP.NET Applications and my own feedbacks. Following some simples tips to gain performances in few changes…

Read the rest of this entry »

Advertisements




Manage your Feeds with RSSToolkit

15 09 2010

A good resource when you need to read or generate feeds like RSS, Atom, OPML or RDF : RSS Toolkit (http://aspnetrsstoolkit.codeplex.com/).

RssToolkit is a complete and simple framework to manipulate your RSS feeds in your .NET projects, from reading to generating them. Moreover, knowledge about Atom or Rss standard is not necessary : you can easily switch from RSS to Atom when generation is required.

Read the rest of this entry »





Simples features for BoundField

26 11 2008

I was surprised the day I wanted to delimit a textbox in row edition(gridview) and avoid the user to enter unnecessary characters in my textbox. A simple boundfield is not fit to support that and custom code is necessary.

So I have written a long time ago a simple class with C#2 that I used when a define some gridview columns : LimitedBoundField.

Read the rest of this entry »





AjaxControlToolkit ScriptManager and webfarms

20 10 2008

Using often AjaxControlToolkit controls on all my web applications (Calendar, popup and others), I have discovered the ScriptManager. Immediatly, I was looking to evaluate what this control can bring me compared to the original from ASP.NET. Only the resources script option which allow to download all resource script in one file was sufficient to convince me. So I have tried it.

All was perfect, but after deploying in production environment with many servers(webfarm) , many errors occurs and some pages would not want to work correctly… I searched during 2-3 days and finally find that AjaxControlToolkit is case sensitive on url. And the root url of my application was changing from lower to upper case url according the server which fulfill the request (it’s a specific case of my entreprise but it can occur elsewhere).

I have found a good resource that explain the issue :
http://plainoldstan.blogspot.com/2008/04/ajaxcontroltoolkit-scriptresourceaxd.html





Web.Config : Configuration by Environment

7 10 2008

During a web project lifecycle, we encounter always the same problem when deploiement is required in development environment(Server A + DB A), Receipt/Qualif (Server B + DB B) and production (Server C + DB C without taking care of web farming). Many developpers choose to create one web.config for each environment; not a bad choice but a permanent synchro is required when adding reference or others commons configurations.
I was looking to improve this system to minimize the synchro time between each web.config file and find a simple tips to exclude the specific data from a web.config : the configSource property.

Read the rest of this entry »





Linq To SQL : How to order with a string typed expression ?

23 09 2008

After using Linq To SQL queries, I was quickly confronted to sort my data with a string expression provided, for example, by a gridview when a user require it. At this moment, I understood that the strongly typed system used with Linq cannot help me for this task… Understanding the fact that allowing a string expression representing an object property is contrary to Linq purpose, I began to search with our best friend “Google” a soluce to resolve quickly my problem. But the best that I can obtain was a “switch case solution” which, according to the string property name, build the linq query sort filter with the associated object property. Immediatly, I guess my switch case code with 8-10 columns… No…

So, I started to write an extended method “OrderBy” allowing to take a string sort expression. Here the result.

Read the rest of this entry »





Hierarchical Gridview

23 09 2008

After using many professional/commercial controls, I was often disappointed in using them. When you are following the simple sample path, it will work. But if you really want to customize the control, the percent rate of bugs and unwanted states increase. If a control cannot be reliable, it’s better to do it and keep a full control on the source code.

That’s why, rather than spending some money to own your multi-gridview and waiting each day after a patch, here is a cool tip to adapt a standard Gridview in hierarchical Gridview:

Create a standard GridView on a webform and create a template field column at the end of the columns collection. Add in the template item : “<tr><td> Second Row </td></tr>”.

That all folk ! When addind this code, the DOM structure of the gridview is broken(Yes, it’s not W3C). But the result is there. Working with IE,Firefox and Chrome.

If we analyse in deep the generating HTML Code of the gridview, we can see the following structure:

<table>

<tr><th>col1</th><th>col second row</th></tr>

<tr><td>col1 value</td><td>

<tr><td>Second Row</td></tr>

</td></tr>

</table>

When adding TR in td(cell of gridview), a new row is created. However, we can manage the visibility of this new row through cell properties. Thus, with an appropriate extender(link,image,other control), we can hide or show the row.

I have created for my personal purpose 2 definition of template Field Column that I can add to a gridview and render it like a Hierarchical Gridview.

Here the code:

 /// <summary>

    /// Build a sub row for each row.

    /// </summary>

    [ToolboxData(@”<{0}:SubRowBoundField></{0}:SubRowBoundField>”)]

    public class SubRowBoundField : TemplateField

    {

        public TextBox ExpandedRowsState

        {

            get;

            set;

        }

 

        public SubRowBoundField()

            : base()

        {

            LeftPadding = new Unit(“20px”);

            TextBox txt = new TextBox();

            txt.ID = “_ExpandedRowsState”;

            ExpandedRowsState = txt;

        }

 

        /// <summary>

        /// Space at the left of the cell (default:20px)

        /// </summary>

        public Unit LeftPadding { get; set; }

 

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)

        {

            base.InitializeCell(cell, cellType, rowState, rowIndex);

            string expandedRowStateValue = (HttpContext.Current.Request.Form[ExpandedRowsState.UniqueID] != null) ? HttpContext.Current.Request.Form[ExpandedRowsState.UniqueID] : ExpandedRowsState.Text;

            if (cellType == DataControlCellType.Header)

            {

                //cell.Controls.Clear();

                //we store the expanded row collection in an hidden field

                ExpandedRowsState.Style.Add(“display”, “none”);

                cell.Controls.Add(ExpandedRowsState);

                //cell.Text = string.Empty;

                cell.Style.Add(“width”, “100px”);

                cell.Style.Add(“display”, “none”);

 

            }

            else if (cellType == DataControlCellType.DataCell)

            {

                GridView g = this.Control as GridView;

                cell.Style.Add(“display”, “none”);

 

                bool displayPlus = (expandedRowStateValue.IndexOf(“[“ + g.PageIndex + “,” + rowIndex + “]”) > -1) ? false : true;

 

                cell.Controls.AddAt(0, new LiteralControl(

                    @”<tr>

                        <td colspan=””100%”” style=””padding-left:” + LeftPadding.ToString() + @”””>

                            <div id=” + g.ClientID + @”_div” + rowIndex + @” 

                                style=””display:” + ((displayPlus)?“none”:“block”) + @”;OVERFLOW: auto;WIDTH:100%”” >”));

                cell.Controls.Add(new LiteralControl(cell.Text));

                cell.Controls.Add(new LiteralControl(@”</div></td></tr>”));

 

            }

        }

    }

 

    /// <summary>

    /// Build a sub row expand-collapse action for each row.

    /// </summary>

    [ToolboxData(@”<{0}:SubRowExtendBoundField></{0}:SubRowExtendBoundField>”)]

    public class SubRowExtendBoundField : TemplateField

    {

        public string CssClassPlus { get; set; }

        public string CssClassMinus { get; set; }

        public TextBox ExpandedRowsState { get; set; }

 

        public SubRowExtendBoundField()

            : base()

        {

            TextBox txt = new TextBox();

            txt.ID = “_ExpandedRowsStateExpander”;

            ExpandedRowsState = txt;

        }

 

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)

        {

            base.InitializeCell(cell, cellType, rowState, rowIndex);

            string expandedRowStateValue = (HttpContext.Current.Request.Form[ExpandedRowsState.UniqueID] != null) ? HttpContext.Current.Request.Form[ExpandedRowsState.UniqueID] : ExpandedRowsState.Text;

            if (cellType == DataControlCellType.Header)

            {

                //we store the expanded row collection in an hidden field

                ExpandedRowsState.Style.Add(“display”, “none”);

                cell.Controls.Add(ExpandedRowsState);

            }

            else if (cellType == DataControlCellType.DataCell)

            {

                GridView g = this.Control as GridView;

                TextBox txtExpanded = g.HeaderRow.Cells[g.Columns.Count – 1].Controls[0] as TextBox;

                string sExpand = @”

                    document.getElementById(‘” + g.ClientID + @”_btnPlus” + rowIndex + @”‘).style.display=’none’;

                    document.getElementById(‘” + g.ClientID + @”_btnMinus” + rowIndex + @”‘).style.display=’block’;

                    document.getElementById(‘” + g.ClientID + @”_div” + rowIndex + @”‘).style.display=’block’;

                    document.getElementById(‘” + ExpandedRowsState.ClientID + @”‘).value=document.getElementById(‘” + ExpandedRowsState.ClientID + @”‘).value+'[“+g.PageIndex+“,” + rowIndex + @”]’;

                    document.getElementById(‘” + txtExpanded.ClientID + @”‘).value=document.getElementById(‘” + txtExpanded.ClientID + @”‘).value+'[“ + g.PageIndex + “,” + rowIndex + @”]’;”;

                string sCollapse = @”

                    document.getElementById(‘” + g.ClientID + @”_btnPlus” + rowIndex + @”‘).style.display=’block’;

                    document.getElementById(‘” + g.ClientID + @”_btnMinus” + rowIndex + @”‘).style.display=’none’;

                    document.getElementById(‘” + g.ClientID + @”_div” + rowIndex + @”‘).style.display=’none’;

                    document.getElementById(‘” + ExpandedRowsState.ClientID + @”‘).value=document.getElementById(‘” + ExpandedRowsState.ClientID + @”‘).value.replace(‘[“ + g.PageIndex + “,” + rowIndex + @”]’,”);

                    document.getElementById(‘” + txtExpanded.ClientID + @”‘).value=document.getElementById(‘” + txtExpanded.ClientID + @”‘).value.replace(‘[“ + g.PageIndex + “,” + rowIndex + @”]’,”);”;

 

                string cancelBubble = @”window.event.cancelBubble=true;window.event.returnValue=false;”;

                sExpand += cancelBubble;

                sCollapse += cancelBubble;

 

 

                bool displayPlus = (expandedRowStateValue.IndexOf(“[“ + g.PageIndex + “,” + rowIndex + “]”) > -1) ? false : true;

 

                LiteralControl lcPlus = new LiteralControl(

                    @”<div id='” + g.ClientID + @”_btnPlus” + rowIndex + @”‘ style=””display:” + ((displayPlus) ? “block” : “none”) + @””” class=” + CssClassPlus + @”

                    onclick=””javascript:” + sExpand + @”””></div>”);

                cell.Controls.Add(lcPlus);

 

                LiteralControl lcMinus = new LiteralControl(

                    @”<div id='” + g.ClientID + @”_btnMinus” + rowIndex + @”‘ style=””display:” + ((!displayPlus) ? “block” : “none”) + @””” class=” + CssClassMinus + @”

                    onclick=””javascript:” + sCollapse + @”””></div>”);

                cell.Controls.Add(lcMinus);

            }

 

        }

 

    }