GridView RowDataBound事件降低了应用程序ASP.NET C#的速度

GridView RowDataBound event slowing down application ASP.NET C#

本文关键字:NET ASP 速度 应用程序 RowDataBound 事件 GridView      更新时间:2023-09-26

我正在构建一个调查应用程序。我有一个页面,可以让管理员、客户、测量师和其他经理查看针对他们/为他们安排的调查。它还显示其状态和其他内容。我在一个gridview Action列中有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次。加载页面需要花费大部分时间。现在,我需要任何替代方案,或者一些改进页面性能的技巧。寻呼和其他事情已经尝试过了。非常感谢。

您尝试过自定义分页吗?默认的网格视图分页只是客户端。

这取决于你的网格视图中有多少条记录。如果您通过数据源获取所有记录,则附加的格式化&转换肯定会降低应用程序的速度。在这种情况下,自定义数据库分页总是首选的,所以您只需要从数据库中获取每页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也可以这样做)。

这将需要一些时间来设置,但结果是值得的。请阅读这篇文章在ASP.NET中自定义分页4家伙从rolla