0

I am building a survey application. I have a page which gives Admin, Clients, Surveyor and other managers a view of surveys scheduled against/for them. It also displays its status and other things. I have 3 image buttons in a gridview Action column. I am on run time binding some styles and Javascript functions to them. This is the event code:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            int clientID = int.Parse(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[5].ToString());
            int surveyID = int.Parse(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[6].ToString());
            int scheduleID = int.Parse(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[0].ToString());
            //string latitude = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[12].ToString();
            //string longitude = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[13].ToString();
            //string address = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[14].ToString();
            string status = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[1].ToString();

            //hdnMapCoordinates.Value += latitude + "|" + longitude + "|" + address + "|" + status + "~";


            List<int> cellsList = new List<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
            for (int i = 0; i < cellsList.Count; i++)
            {
                e.Row.Cells[cellsList[i]].Style.Add("cursor", "pointer");
                e.Row.Cells[cellsList[i]].CssClass = "inline";
                e.Row.Cells[cellsList[i]].Attributes.Add("href", "#inline_content3");
                e.Row.Cells[cellsList[i]].Attributes.Add("onclick", string.Format("OpenForm({0},{1},{2},'{3}'); return false;", surveyID, clientID, scheduleID, status));
            }


            System.Web.UI.WebControls.Image imgStatus = (System.Web.UI.WebControls.Image)e.Row.FindControl("imgStatus");
            ((ImageButton)e.Row.FindControl("imgOpenSurvey")).OnClientClick = string.Format("OpenSurvey({0} , {1} , {2});return false;", surveyID, clientID, scheduleID);
            ((ImageButton)e.Row.FindControl("imgApprove")).OnClientClick = string.Format("ApproveSurvey({0});return false;", scheduleID);

            if (userRole.Contains("Supervisor"))
            {
                if (status == "submitted")
                {
                    ((ImageButton)e.Row.FindControl("imgApprove")).Visible = true;
                }
            }

            ((ImageButton)e.Row.FindControl("imgOpenSurvey")).Style.Add("display", "none");

            if (status == "new" || status == "NEW" || status == "scheduled")
            {
                imgStatus.ImageUrl = "~\\Images\\new.png";
                imgStatus.ToolTip = "new";
            }
            else if (status == "submitted")
            {
                imgStatus.ImageUrl = "~\\Images\\approve-required.png";
                imgStatus.ToolTip = "submitted";
            }
            else if (status == "approved")
            {
                ((ImageButton)e.Row.FindControl("imgOpenSurvey")).Style.Add("display", "");
                ((ImageButton)e.Row.FindControl("imgPrintSurvey")).Style.Add("display", "");

                imgStatus.ImageUrl = "~\\Images\\checkmark.png";
                imgStatus.ToolTip = "approved";
            }
            else if (status == "seen")
            {
                ((ImageButton)e.Row.FindControl("imgOpenSurvey")).Style.Add("display", "");
                ((ImageButton)e.Row.FindControl("imgPrintSurvey")).Style.Add("display", "");

                imgStatus.ImageUrl = "~\\Images\\checkmark.png";
                imgStatus.ToolTip = "approved";
                //e.Row.BackColor = Color.FromArgb(153, 255, 153);
            }
            else if (status == "on-hold")
            {
                imgStatus.ImageUrl = "~\\Images\\close-btn.png";
                imgStatus.ToolTip = "On-Hold";
            }
            else if(status == "canceled"){
                imgStatus.ImageUrl = "~\\Images\\cancel.png";
                imgStatus.ToolTip = "Canceled";
            }
        }
    }

Now issue is when I ran ANTS Performance Profiler 7.1 against this page and found that this event is hit 297 times. Which is taking most time to load a page. Now I need any alternative to this or some tips for improvement in page's performance. Paging and other things have already been tried. Thank you.

FreshDev
  • 328
  • 1
  • 3
  • 18
  • Not really very meaningful in terms of performance but why not just do `for (int i = 0; i < 7; i++)`, and then `e.Row.Cells[i]`? All cellsList is is a list of consecutive integers... – ElGavilan Aug 28 '14 at 12:24
  • 1
    Why do you always copy-paste the same, use variables instead. For example: `DataRow row = ((System.Data.DataRowView)e.Row.DataItem).Row;`. Now you can always access this variable. That is more readable, maintainable and also more efficient. The same is true for `e.Row.Cells[cellsList[i]]`. Apart from that, use the correct datatype in the first place instead of converting all to string and then via `int.Parse` back to int. Use `int id = row.Field(5)`. – Tim Schmelter Aug 28 '14 at 12:26
  • @Tim, Ok I will do that. But will this improve the processing? It will only make code more readable I think. – FreshDev Aug 28 '14 at 14:33
  • @FreshDev: i don't know if that is noticeably more efficient but it can be. It definitely increases readability but is also more efficient. Depends on the number of rows and cells. – Tim Schmelter Aug 28 '14 at 14:42

1 Answers1

0

Have you tried custom paging? Default gridview paging is only client side.

It depends on how many records there are in your gridview. If you are fetching all records through your datasource, the additional formatting & conversion will definitely slow your down application. In such scenarios custom database paging is always preferred, so you would just fetch 10 records per page from the database & apply formatting to only those.

SELECT ...
FROM
   (SELECT ... 
         ROW_NUMBER() OVER(ORDER BY ColumnName) as RowNum
    FROM Employees e
   ) as DerivedTableName
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1

Here you would pass the startRowIndex of your page (gridview would pass it automatically), the maximum rows you want to fetch per page(gridview does this as well).

It would take some time to setup but results will be worthwhile. Please review this article Custom paging in ASP.NET 4 guys from rolla

Zo Has
  • 12,599
  • 22
  • 87
  • 149