【问题标题】:GridView RowDataBound event slowing down application ASP.NET C#GridView RowDataBound 事件减慢应用程序 ASP.NET C#
【发布时间】:2014-08-28 12:20:18
【问题描述】:

我正在构建一个调查应用程序。我有一个页面,可以让管理员、客户、测量员和其他经理查看针对/为他们安排的调查。它还显示其状态和其他内容。我在 gridview 操作列中有 3 个图像按钮。我在运行时将一些样式和 Javascript 函数绑定到它们。这是事件代码:

    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";
            }
        }
    }

现在的问题是当我对这个页面运行 ANTS Performance Profiler 7.1 并发现这个事件被点击了 297 次。加载页面花费的时间最多。现在我需要任何替代方法或一些改进页面性能的技巧。分页和其他事情已经尝试过了。 谢谢。

【问题讨论】:

  • 在性能方面并不是很有意义,但为什么不先做for (int i = 0; i &lt; 7; i++),然后再做e.Row.Cells[i]?所有的 cellsList 都是一个连续整数的列表...
  • 为什么你总是复制粘贴一样,用变量代替。例如:DataRow row = ((System.Data.DataRowView)e.Row.DataItem).Row;。现在您可以随时访问此变量。这更具可读性、可维护性和效率。 e.Row.Cells[cellsList[i]] 也是如此。除此之外,首先使用正确的数据类型,而不是将全部转换为字符串,然后通过int.Parse 转换回 int。使用int id = row.Field&lt;int&gt;(5)
  • @Tim,好的,我会这样做的。但这会改善处理吗?我认为它只会使代码更具可读性。
  • @FreshDev:我不知道这是否明显更有效,但可以。它肯定会增加可读性,但也更有效。取决于行数和单元格数。

标签: c# javascript asp.net gridview ants


【解决方案1】:

您是否尝试过自定义分页?默认的gridview分页只是客户端。

这取决于您的网格视图中有多少条记录。 如果您通过数据源获取所有记录,那么额外的格式和转换肯定会减慢您的应用程序。 在这种情况下,自定义数据库分页始终是首选,因此您只需从数据库中每页获取 10 条记录并仅对这些记录应用格式。

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

在这里,您将传递页面的 startRowIndex(gridview 会自动传递它),您希望每页获取的最大行数(gridview 也这样做)。

设置需要一些时间,但结果是值得的。请查看这篇文章Custom paging in ASP.NET 4 guys from rolla

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2018-08-13
    • 2011-05-24
    相关资源
    最近更新 更多