改善Web控件的加载

时间:2016-05-17 13:31:57

标签: c# asp.net

我是一个模态表单,我在这里展示文章。

在我的手柄中,我有一个标签,对于标签的每一行,我有8个网页控件(5个ImageButton,1个LinkBut​​ton,1个TextBox,1个DropDownList)和一个事件处理程序。

例如,如果我的文件中有98篇文章,我将有98行和784(98 * 8)个网页控件。加载花了太多时间。

我怎样才能改善装载效果?

以下是构建标签的代码:

foreach (LignePanier lp in liste)
            {
                TableRow tr = new TableRow();
                tr.CssClass = "lignePanier";
                tr.Width = Unit.Percentage(100);
                tr.Attributes["style"] = "background-color:" + couleur;

                TableCell celluleLogo = new TableCell();
                celluleLogo.CssClass = "celluleTabArticle";
                celluleLogo.Width = Unit.Percentage(5);
                celluleLogo.Attributes["style"] = "text-align:center;";

                if (!String.IsNullOrEmpty(lp._ARTICLE._FOURNISSEUR._LOGO))
                {
                    System.Web.UI.WebControls.ImageButton logoF = new System.Web.UI.WebControls.ImageButton();
                    logoF.ImageUrl = ToolsImage.obtenirUrl(Request.Url.ToString()) + lp._ARTICLE._FOURNISSEUR._LOGO;
                    logoF.ID = "logoF" + i;
                    logoF.Width = Unit.Pixel(50);
                    logoF.Height = Unit.Pixel(50);
                    logoF.Click += new ImageClickEventHandler(logoF_Click);

                    celluleLogo.Controls.Add(logoF);
                }

                TableCell tcITMREF = new TableCell();
                tcITMREF.CssClass = "cellulePanier";
                tcITMREF.Width = Unit.Percentage(7);
                tcITMREF.Text = lp._ARTICLE._ITMREF;

                TableCell tcFAM = new TableCell();

                if (j == 0)
                {
                    tcFAM.CssClass = "cellulePanier";
                    tcFAM.Width = Unit.Percentage(6);
                    tcFAM.RowSpan = item.Count();

                    tcFAM.Text = lp._ARTICLE._FAM1._FAM1 + "-" + lp._ARTICLE._FAM2._FAM2 + "-" + lp._ARTICLE._FAM3._FAM3 +
                        "-" + lp._ARTICLE._FAM4._FAM4;

                    String chaineFamille = "Famille 1 : " + lp._ARTICLE._FAM1._FAM1 + " (" + lp._ARTICLE._FAM1._FAMDES + ") \n";
                    chaineFamille += "Famille 2 : " + lp._ARTICLE._FAM2._FAM2 + " (" + lp._ARTICLE._FAM2._FAMDES + ") \n";
                    chaineFamille += "Famille 3 : " + lp._ARTICLE._FAM3._FAM3 + " (" + lp._ARTICLE._FAM3._FAMDES + ") \n";
                    chaineFamille += "Famille 4 : " + lp._ARTICLE._FAM4._FAM4 + " (" + lp._ARTICLE._FAM4._FAMDES + ")";

                    tcFAM.ToolTip = chaineFamille;
                }

                TableCell tcLIBLONFOUR = new TableCell();
                tcLIBLONFOUR.CssClass = "cellulePanier";
                tcLIBLONFOUR.Width = Unit.Percentage(21);

                Label lb = new Label();
                lb.Text = lp._ARTICLE._LIBLONFOUR.Split('[')[0];
                lb.Attributes["style"] = "text-decoration:underline;";

                Label label = new Label();
                label.Text = "<br/>" + lp._ARTICLE.obtenirChaineVenduParPourCommandeType();

                LinkButton lbLIBLONFOUR = new LinkButton();
                lbLIBLONFOUR.ID = "lbLIBLONFOUR" + i;
                lbLIBLONFOUR.Controls.Add(lb);
                lbLIBLONFOUR.Controls.Add(label);
                lbLIBLONFOUR.Click += new EventHandler(liblonfour_Click);

                tcLIBLONFOUR.Controls.Add(lbLIBLONFOUR);

                TableCell tcDelai = new TableCell();
                tcDelai.CssClass = "cellulePanier";
                tcDelai.Width = Unit.Percentage(4);
                tcDelai.Text = lp._ARTICLE._LTI + " j";
                tcDelai.Attributes["Style"] = "text-align:center";

                TableCell tcPANQTVVAL = new TableCell();
                tcPANQTVVAL.CssClass = "cellulePanier";
                tcPANQTVVAL.Width = Unit.Percentage(8);
                tcPANQTVVAL.Attributes["Style"] = "text-align:left";

                TextBox txtPANQTVVAL = new TextBox();
                txtPANQTVVAL.ID = "txt" + i;
                txtPANQTVVAL.AutoPostBack = true;
                txtPANQTVVAL.Width = Unit.Pixel(30);
                txtPANQTVVAL.CssClass = "txtLignePanier";
                txtPANQTVVAL.TextChanged += new EventHandler(txtQTE_TextChanged);

                if (lp._PANQTVVAL == 0)
                {
                    txtPANQTVVAL.Attributes["style"] = "color:Red;";
                }

                if (lp._CREDAT == maxCREDAT)
                {
                    txtPANQTVVAL.Attributes["style"] += "background-color:#8BEAB7";
                }

                Label labelLIBCOND = new Label();
                labelLIBCOND.Text = lp._ARTICLE._LIBVTE;

                ImageButton buttonMoins = new ImageButton();
                buttonMoins.CssClass = "buttonMoins";
                buttonMoins.ID = "buttonMoins" + i;
                buttonMoins.Click += new ImageClickEventHandler(buttonMoins_Click);
                buttonMoins.ImageUrl = "~/App_Themes/eWare_Theme/images/moins.png";

                ImageButton buttonPlus = new ImageButton();
                buttonPlus.CssClass = "buttonPlus";
                buttonPlus.ID = "buttonPlus" + i;
                buttonPlus.Click += new ImageClickEventHandler(buttonPlus_Click);
                buttonPlus.ImageUrl = "~/App_Themes/eWare_Theme/images/plus.png";

                if (lp._PANQTVVAL == 0)
                {
                    txtPANQTVVAL.Text = "0";
                    this.buttonSubmit.Enabled = false;
                }
                else
                {
                    txtPANQTVVAL.Text = ToolsString.obtenirNombreArrondi(lp._PANQTVVAL, 2);
                }

                tcPANQTVVAL.Controls.Add(txtPANQTVVAL);
                tcPANQTVVAL.Controls.Add(labelLIBCOND);

                TableCell tcButton = new TableCell();
                tcButton.CssClass = "cellulePanier";
                tcButton.Attributes["style"] = "text-align:left;";
                tcButton.Width = Unit.Percentage(2);

                tcButton.Controls.Add(buttonPlus);
                tcButton.Controls.Add(buttonMoins);

                TableCell tcPANPRIUF = new TableCell();
                tcPANPRIUF.CssClass = "cellulePanier";
                tcPANPRIUF.Width = Unit.Percentage(7);
                tcPANPRIUF.Text = ToolsString.obtenirNombreArrondi(lp._ARTICLE._PRI * lp._ARTICLE._COEFCOND, 4) + " €";
                tcPANPRIUF.Attributes["Style"] = "text-align:left";

                TableCell tcTotal = new TableCell();
                tcTotal.CssClass = "cellulePanier";
                tcTotal.Width = Unit.Percentage(7);
                tcTotal.Attributes["Style"] = "text-align:left";

                decimal total = lp._PANQTVVAL * lp._ARTICLE._PRI * lp._ARTICLE._COEFCOND;

                tcTotal.Text = ToolsString.obtenirNombreArrondi(total, 2) + " €";

                TableCell tcPoubelle = new TableCell();
                tcPoubelle.Width = Unit.Percentage(27);
                tcPoubelle.ToolTip = "A l'entrepôt le " + lp._RCPDAT.ToString("D", CultureInfo.CurrentCulture);

                DropDownList ddl = new DropDownList();
                ddl.ID = "ddl" + i;
                ddl.AutoPostBack = true;
                ddl.SelectedIndexChanged += new EventHandler(listeLivraisonPanier_SelectedIndexChanged);

                if (lp._ARTICLE._PRODINDISPO == 0)
                {
                    List<String> _liste = MgtDateLivraison.obtenirInstance().obtenirJourLivraison(lp._ARTICLE._FOURNISSEUR._FOURNISSEUR, heureLimite, lp._ARTICLE._LTI);

                    if (lp._DLVRCPDAT < DateTime.Parse(_liste[0].Split('*')[0]))
                    {
                        String s = lp._DLVRCPDAT.ToString("D",
                  CultureInfo.CreateSpecificCulture("fr-FR"));

                        ddl.Items.Add(new System.Web.UI.WebControls.ListItem(s, s));
                    }

                    foreach (String s in _liste)
                    {
                        ddl.Items.Add(new System.Web.UI.WebControls.ListItem(s, s));
                    }

                    //Si ma date est inférieur au premier élément de ma liste, c'est que je suis hors borne
                    if (lp._DLVRCPDAT < DateTime.Parse(_liste[0].Split('*')[0]))
                    {
                        ddl.CssClass = "dateHorsBorne";
                        //this.buttonSubmit.Enabled = false;
                    }
                }

                if (ddl.Items.FindByValue(lp._DLVRCPDAT.ToString("D",
                  CultureInfo.CreateSpecificCulture("fr-FR"))) != null)
                {
                    ddl.SelectedValue = lp._DLVRCPDAT.ToString("D",
                  CultureInfo.CreateSpecificCulture("fr-FR"));
                }
                else
                {
                    ddl.SelectedValue = lp._DLVRCPDAT.ToString("D",
                  CultureInfo.CreateSpecificCulture("fr-FR")) + "*";
                }

                ImageButton buttonDuplicateRow = new ImageButton();
                buttonDuplicateRow.ID = "buttonDuplicateRow" + i;
                buttonDuplicateRow.CssClass = "buttonPlus";
                buttonDuplicateRow.ToolTip = "Dupliquer la ligne";
                buttonDuplicateRow.Click += new ImageClickEventHandler(buttonDuplicateRow_Click);
                buttonDuplicateRow.ImageUrl = "~/App_Themes/eWare_Theme/images/plus.png";

                ImageButton buttonSupprimer = new ImageButton();
                buttonSupprimer.ID = "buttonSupprimer" + i;
                buttonSupprimer.CssClass = "buttonSupprimer";
                buttonSupprimer.OnClientClick = "return supprimerArticlePanier();";
                buttonSupprimer.Click += new ImageClickEventHandler(buttonSupprimer_Click);
                buttonSupprimer.ImageUrl = "~/App_Themes/eWare_Theme/images/logo_delete.png";

                tcPoubelle.Controls.Add(ddl);
                tcPoubelle.Controls.Add(buttonDuplicateRow);
                tcPoubelle.Controls.Add(buttonSupprimer);

                TableCell tcInfos = new TableCell();
                tcInfos.Text = lp._ARTICLE._FOURNISSEUR._FOURNISSEUR + "." + lp._ARTICLE._ITMREF + "." +
                    lp._DLVRCPDAT.ToString().Split('*')[0] + "." + lp._RCPDAT.ToString() + "." + lp._ARTICLE._VENDUPAR +
                    "." + lp._ARTICLE._LIBCOND;

                tcInfos.Attributes["style"] = "display:none";

                //Si j == 0, je suis sur un nouveau regroupement, j'ajoute donc ma cellule à ma ligne.
                if (j == 0)
                {
                    tr.Cells.Add(tcFAM);

                    j = 1;
                }

                tr.Cells.Add(celluleLogo);

                tr.Cells.Add(tcITMREF);
                tr.Cells.Add(tcLIBLONFOUR);
                tr.Cells.Add(tcPANQTVVAL);
                tr.Cells.Add(tcButton);
                tr.Cells.Add(tcPANPRIUF);
                tr.Cells.Add(tcTotal);
                tr.Cells.Add(tcDelai);
                tr.Cells.Add(tcPoubelle);
                tr.Cells.Add(tcInfos);

                tabPanier.Rows.Add(tr);

                i++;
            }

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

为什么不使用转发器控件,而不是以编程方式构建所有表格单元格并将它们添加到控件集合中?它们相当轻巧,从性能的角度来看,转发器中的100行不应成为问题。

        <asp:Repeater runat="server" ID="rptMyData">
            <HeaderTemplate>
                <table>
                    <thead>
                        <tr>
                            <th>Field1</th>
                            <th>Field2</th>
                            <th>Field3</th>
                            <th>Field4</th>
                        </tr>
                    </thead>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><asp:ImageButton runat="server" ImageUrl="<%# ((MyObject)Container.DataItem).Image1Url %>" /></td>
                    <td><asp:ImageButton runat="server" ImageUrl="<%# ((MyObject)Container.DataItem).Image2Url %>" /></td>
                    <td><asp:ImageButton runat="server" ImageUrl="<%# ((MyObject)Container.DataItem).Image3Url %>" /></td>
                    <td><%# ((MyObject)Container.DataItem).SomeTextProperty %></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>

然后在你的代码隐藏中:

List<MyObject> myObjects = SomeClass.GetData();
rptMyData.DataSource = myObjects;
rptMyData.DataBind();

关于中继器的文档很多。