ItemDataBound减慢了我的页面渲染速度

时间:2017-08-29 21:11:54

标签: c# asp.net

1我在这里的第一个问题,请原谅,如果我不熟练使用技术语言,因为我是来自莫桑比克深处的自学开发人员,感谢这个网站。

我的问题是我有一个

public void topicView_ItemDataBound(object sender,e)

在我的代码后面有一个带有转发器的特定aspx页面,它接收来自数据表的信息(如果我的术语是正确的。)ItemDataBound事件是将我的控件安排在转发器中,以便隐藏,禁用和根据确定的标准显示其他控件。现在我注意到ItemDataBound事件正在减慢我的页面加载时间大约20到40秒,这真的很糟糕,即使在回发后也是如此。当我删除I​​temDataBound事件。我运行顺利。但我无法处理ItemDataBound事件,因为它是我知道如何安排具有交替条件的转发器的唯一方法。这是一个快速回答的常见问题,还是我应该发布我的完整代码?这只发生在此事件的2页上。我正在使用c#,如果有任何帮助。 net.4.5

public void topicView_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // Show or hid div here
            HiddenField MediaType = (HiddenField)e.Item.FindControl("MediaType");
            HiddenField PageAdmin = (HiddenField)e.Item.FindControl("PageAdmin");
            HiddenField f1 = (HiddenField)e.Item.FindControl("F1");
            HiddenField PP = (HiddenField)e.Item.FindControl("PP");
            HiddenField isread= (HiddenField)e.Item.FindControl("isread");
            HiddenField tread = (HiddenField)e.Item.FindControl("tread");

            // Label Label2 = (Label)e.Item.FindControl("Label2");
            // Label2.Text = myDDL.Value;
            System.Web.UI.HtmlControls.HtmlContainerControl image_video = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("image_video");

            System.Web.UI.HtmlControls.HtmlContainerControl image_pic = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("image_pic");
            System.Web.UI.HtmlControls.HtmlContainerControl PP1 = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("PP1");
            System.Web.UI.HtmlControls.HtmlContainerControl userpic = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("userpic");
            System.Web.UI.HtmlControls.HtmlContainerControl compic = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("compic");
            System.Web.UI.HtmlControls.HtmlContainerControl userpic2 = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("userpic2");
            System.Web.UI.HtmlControls.HtmlContainerControl compic2 = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("compic2");
            System.Web.UI.HtmlControls.HtmlContainerControl pf = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("pf");
            System.Web.UI.HtmlControls.HtmlContainerControl attach = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("attach");

            LinkButton LinkButton3 = (LinkButton)e.Item.FindControl("LinkButton3");
            LinkButton LinkButton1 = (LinkButton)e.Item.FindControl("LinkButton1");
            Label Label4 = (Label)e.Item.FindControl("Label4");
            Label Label10 = (Label)e.Item.FindControl("Label10");

            System.Web.UI.WebControls.Image readsign = (System.Web.UI.WebControls.Image)e.Item.FindControl("readsign");
            System.Web.UI.WebControls.Image Image2 = (System.Web.UI.WebControls.Image)e.Item.FindControl("Image2");

            if (MediaType.Value == "video")
            {
                image_video.Visible = false;
                image_pic.Visible = true;
                Image2.ImageUrl = "~/images/readmail.png";

            }


            if (MediaType.Value == "image")
            {
                image_video.Visible = true;
                image_pic.Visible = false;

            }

            if (MediaType.Value == "" ) { attach.Visible = false; } else { attach.Visible = true; }

            if (PageAdmin.Value == "False")
            {
                compic.Visible = false;
                userpic.Visible = true;
                userpic2.Visible = true;
                compic2.Visible = false;
            }
            if (PageAdmin.Value == "True")
            {
                userpic.Visible = false;
                compic.Visible = true;
                userpic2.Visible = false;
                compic2.Visible = true;
            }

            if (isread.Value == "True")
            {
                readsign.ImageUrl = "~/images/readmail.png";
                Label4.Text = "foi lido ";
                Label10.Text = tread.Value;
             }
     }

}

和我的转发器的aspx代码:

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
 OnItemDataBound="topicView_ItemDataBound"><ItemTemplate> 
 <div id="messages" style="margin-bottom: 10px">
    <asp:HiddenField ID="Status" runat="server" 
 Value='<%#""+Eval("Isreplyed") %>' />
    <asp:HiddenField ID="Pageadmin" runat="server" 
 Value='<%#""+Eval("PageAdmin") %>'/>
    <asp:HiddenField ID="MediaType" runat="server" 
Value='<%#""+Eval("MediaType") %>' />
    <asp:HiddenField ID="isread" runat="server" Value='<%#""+Eval("Isread") 
 %>' />
    <asp:HiddenField ID="tread" runat="server" Value='<%#""+Eval("tread","
 {0:d / MM " + "@" + " HH:mm}") %>' />
    <div id="Omsg" style="padding: 5px; border: thin solid #FFFFFF; box-
 shadow:rgba(255, 255, 255,0.9) 0 0 7px; background-color: #FFFFFF;border-
 radius:5px; " >
 <table style="width: 100%; text-align: left; margin-left: 0px;">
  <tr><td colspan="2">
      <asp:Image ID="readsign" runat="server" Height="25" Width="25" 
ImageUrl="~/images/newmail.png" />
      <asp:Label ID="Label4" runat="server" Text='<%# ""+Eval("Isreplyed") 
%>' Font-Size="X-Small" ForeColor="#999999"></asp:Label>&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label10" runat="server" Text='<%#""+Eval("datesent","{0:d / 
MM " + "@" + " HH:mm}") %>' Font-Size="X-Small" ForeColor="#999999">
</asp:Label>
      </td></tr>         
   <tr>

                <td style="width: 66px; text-align: left;"><a id="pps" ><div 
id="compic" ClientIDMode="Static" runat="server"><img id="PP1" alt="" 
class="img-rounded" src='<%#"/ProfilePictures/"+Eval("logo") %>' 
style="width: 50px; height: 50px" /></div><input id="Hidden2" type="hidden" 
runat="server" value='<%#"/ProfilePictures/"+Eval("logo") %>' /></a>
                    <div id="userpic" clientidmode="Static" runat="server">

                        <img alt="" class="img-circle" 
src='<%#"/ProfilePictures/"+Eval("ProfilePicture") %>' style="width: 50px; 
height: 50px" />
                    </div><input id="Hidden3" type="hidden" runat="server" 
value='<%#"/ProfilePictures/"+Eval("ProfilePicture") %>' />

                     </td>
                <td style="line-height: 15px"> <div id="compic2" 
ClientIDMode="Static" runat="server"> <a  href='<%#"/Landing.aspx?
Restid="+Eval("id") %>' ><asp:Label ID="Label2" class="head" runat="server" 
Text='<%# ""+Eval("name") %>' Font-Size="Medium" ForeColor="#B9A47B">
</asp:Label> </a><a/><br/>
                      <asp:Label ID="CName" runat="server" 
Text='<%#""+Eval("slogan") %>' Font-Size="Smaller"></asp:Label>.   ...</div>
                     <div id="userpic2" ClientIDMode="Static" 
runat="server"><a  href='<%#"/Landing.aspx?Restid="+Eval("Username") %>' >
<asp:Label ID="Label8" class="head" runat="server" 
Text='<%#""+Eval("Username") %>' Font-Size="Large" ForeColor="#B9A47B">
</asp:Label> <a/><br/>
                      <asp:Label ID="Label9" runat="server" 
Text='<%#""+Eval("Job") %>' Font-Size="Smaller"></asp:Label></a>
                         <br />
                         <a/>
<a href='<%#"notificationmaster.aspx?BlogId="+Eval("Blogid") %>'> <asp:Label 
ID="Label5" runat="server" Text='<%# "Respondendo à publicação :  
"+Eval("BlogTitle") %>' Font-Size="smaller" ForeColor="#B9A47B"></asp:Label>
</a><br/>


                    </div>
                   <a/><span style="font-size: x-small; color: #999999"> 
enviado : </span><asp:Label ID="Label3" runat="server" 
Text='<%#""+Eval("datesent","{0:d MMMM yyyy - HH:mm}") %>' Font-Size="X-
Small" ForeColor="#999999"></asp:Label></a>
                    <br />
                   <div id="attach" ClientIDMode="Static" runat="server" > 
<asp:Label ID="Label17" runat="server" Text="Esta mensagem tem anexo" 
ForeColor="#999999" Font-Size="X-Small" Font-Underline="True">
</asp:Label>&nbsp; <img alt="" src="images/attach.png" style="width: 20px; 
height: 20px" /></div>
                </td>



            </tr>
  <tr>
 <td colspan="2">
&nbsp;
<br/>
<div id="msgbody" style="display: none">
<asp:Label ID="Label1" runat="server" Text='<%#""+Eval("mbody") %>'>
</asp:Label><br/>
 <div id="image_pic" ClientIDMode="Static" runat="server" >
            <asp:HiddenField ID="HiddenField1" runat="server" 
Value='<%#"/PostImages/"+Eval("image") %>' />
        <asp:Image ID="Image1" class="img-thumbnail" ClientIDMode="Static" 
runat="server" ImageUrl='<%#"/PostImages/"+Eval("image") %>' alt="Broken" 
Width="100%" />
           </div>
        <div id="image_video" runat="server" onclick="AddView" >
           <video id="PostVedio" runat="server" controls 
poster="/images/chimoioonline.png" src='<%#"/video/"+Eval("video") %>' 
style="width: 100%">
<source src="demo.mp4" type="video/mp4" />
<source src="demo.webm" type="video/webm"/>
<source src="demo.ogv" type="video/ogg"/>
<source src="demo.ogv" type="video/avi"/>             
<p>Fallback code if video isn't supported</p>/
</video></div></div>

 </td>
  </tr>
  <tr>
      <td colspan="2" style="text-align: right">
          <hr style="padding: 2px; margin: 5px" />

          <div id="deletm" style="display: inline-block">
              <input id="Hidden1" type="hidden" runat="server" 
 value='<%#Eval("id")%>' />
          <asp:LinkButton ID="LinkButton3" runat="server" CssClass="btn" 
 onclick="pmdelet" BorderColor="#CCCCCC" BorderWidth="1px"><img 
 src="/images/delete.png" alt="" style=" height: 15px" />
 </asp:LinkButton>&nbsp;&nbsp;<br />
              </div> 
          <div id="sendpm" style="display: inline-block">
              <input id="mido" type="hidden" runat="server" 
 value='<%#Eval("id")%>' />
              <input id="Hidden4" type="hidden" runat="server" 
value='<%#Eval("BlogId")%>' />
              <input id="sender2" type="hidden" runat="server" 
value='<%#Eval("sender")%>' />
              <asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn" 
BorderColor="#CCCCCC" BorderWidth="1px" OnClientClick="" 
PostBackUrl='<%#"PMS.aspx?id=" + Eval("id") + "&BlogId=" + Eval("BlogId")+ 
"&sender=" + Eval("sender")%>' ><img src="images/pvtemail.png" alt="" 
style=" height: 15px" /></asp:LinkButton>&nbsp;&nbsp;
          </div>
          <div id="read" style="display: inline-block">
              <input id="H5" type="hidden" runat="server" 
value='<%#Eval("id")%>' />
              <input id="H7" type="hidden" runat="server" 
value='<%#"/ProfilePictures/"+Eval("logo") %>' />
              <input id="H8" type="hidden" runat="server" 
value='<%#"/ProfilePictures/"+Eval("ProfilePicture") %>' />
              <input id="H6" type="hidden" runat="server" 
value='<%#Eval("Isreplyed")%>' />
              <input id="H14" type="hidden" runat="server" 
value='<%#Eval("Username")%>' />
              <input id="H10" type="hidden" runat="server" 
value='<%#Eval("name")%>' />
              <input id="H13" type="hidden" runat="server" 
value='<%#Eval("Subject")%>' />
              <input id="H9" type="hidden" runat="server" 
value='<%#Eval("BlogTitle")%>' />
              <input id="H11" type="hidden" runat="server" 
value='<%#Eval("datesent","{0:d / MM " + "@" + " HH:mm}")%>' />
              <input id="H12" type="hidden" runat="server" 
value='<%#Eval("mbody")%>' />
              <input id="v5" type="hidden" runat="server" value='<%# 
Eval("video")%>' />
              <input id="p5" type="hidden" runat="server" 
value='<%#Eval("image")%>' />
               <input id="sender" type="hidden" runat="server" 
value='<%#Eval("sender")%>' />
               <input id="Hidden5" type="hidden" runat="server" 
value='<%#Eval("Blogid")%>' />
               <input id="PageAdmin2" type="hidden" runat="server" 
value='<%#Eval("PageAdmin")%>' />
              <asp:LinkButton ID="LinkButton2" runat="server" CssClass="btn"  
BorderColor="#CCCCCC" BorderWidth="1px" OnClientClick="return false"><img 
src="/images/read2.png" alt="" style=" height:15px" /></asp:LinkButton>

          </div>

      </td>
  </tr>
            </table>
     </div>

    <div id="Rmsg">

    </div>


</div>
</ItemTemplate></asp:Repeater>

My Server profiler screen shot

1 个答案:

答案 0 :(得分:0)

这不是一个真正的答案,但更多的方法让你深入研究这个问题是什么。 (而且它还要征求意见)

首先,您需要确保安装了SQL Server管理工具,特别是您需要名为SQL server profiler的工具。

在分析器中,您可以转到&#34; file&#34; &#34;新痕迹&#34;并连接到您的SQL服务器。

在Trace属性中,您只选择&#34; RPC:completed&#34;和&#34; SQL:BatchCompleted&#34;。

现在您可以运行您的aspx页面了,您应该在分析器中查看页面生成的所有SQL查询。你应该注意两件事。

首先,查找长时间运行的查询。持续时间列在MS中,您不应该有超过1000毫秒的任何人。

其次,运行页面时运行的查询数。普通页面不应超过20个查询。通常当你遇到问题时,你可以运行数百个。