簡體   English   中英

GridView RowDataBound事件使應用程序ASP.NET C#變慢

[英]GridView RowDataBound event slowing down application ASP.NET C#

我正在構建一個調查應用程序。 我有一個頁面,可為管理員,客戶,測量師和其他經理提供針對/針對他們進行的調查的視圖。 它還顯示其狀態和其他內容。 我在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次。 最多花費時間來加載頁面。 現在,我需要任何替代方法或一些技巧來改善頁面性能。 分頁和其他操作已經嘗試過。 謝謝。

您是否嘗試過自定義分頁? 默認的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也會這樣做)。

設置會花費一些時間,但是結果值得。 請閱讀本文,了解來自Rolla的ASP.NET 4人員中的自定義頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM