如何识别定时器的回调(定时器在主页面中)同时在Master和&内容页

时间:2011-10-26 09:28:04

标签: c# asp.net timer master-pages

我的网页基于asp.net中的主页和内容页面,带有c# 我在主页面中有一个计时器,如下所示:

                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:Timer runat="server" ID="Timer1" Interval="10000" Enabled="False">
                            </asp:Timer>
                            <div id="SiteStatistics">
                                <asp:Label ID="lblDownload_Count_Title" runat="server" Text="Download Count :" ToolTip="Download Count :"
                                    CssClass="lblTitleInStatistics"></asp:Label>
                                <asp:Label ID="lblDownload_Count" runat="server" Text="<%# Download_Count() %>" CssClass="lblCountInStatistics"></asp:Label>
                                <br />
                                <asp:Label ID="lblDownload_Count_By_UserID_Title" runat="server" Text="Ur Download Count :"
                                    ToolTip="Your Download Count From The Begining Of Registration UpTo Now" CssClass="lblTitleInStatistics"></asp:Label>
                                <asp:Label ID="lblDownload_Count_By_UserID" runat="server" Text="<%# Download_Count_By_UserID() %>"
                                    CssClass="lblCountInStatistics"></asp:Label>
                                <br />
                                <asp:Label ID="lblDownload_Count_By_UserID_Today_Title" runat="server" Text="Ur Download Count-Today :"
                                    ToolTip="Your Download Count-Today" CssClass="lblTitleInStatistics"></asp:Label>
                                <asp:Label ID="lblDownload_Count_By_UserID_Today" runat="server" Text="<%# Download_Count_By_UserID_Today() %>"
                                    CssClass="lblCountInStatistics"></asp:Label>
                                <br />
                                <asp:Label ID="lblDownload_Size_By_UserID_Title" runat="server" Text="Ur Download Size :"
                                    ToolTip="Your Download Size From The Begining Of Registration UpTo Now" CssClass="lblTitleInStatistics"></asp:Label>
                                <asp:Label ID="lblDownload_Size_By_UserID" runat="server" Text="<%# Download_Size_By_UserID() %>"
                                    CssClass="lblCountInStatistics"></asp:Label>
                                <br />
                                <asp:Label ID="lblDownload_Size_By_UserID_Today_Title" runat="server" Text="Ur Download Size-Today :"
                                    ToolTip="Your Download Size-Today" CssClass="lblTitleInStatistics"></asp:Label>
                                <asp:Label ID="lblDownload_Size_By_UserID_Today" runat="server" Text="<%# Download_Size_By_UserID_Today() %>"
                                    CssClass="lblCountInStatistics"></asp:Label>
                                <br />
                                <asp:Label ID="lblDownload_Size_By_UserID_Today_Remain_Title" runat="server" Text="Ur Remain Download Size-Today :"
                                    ToolTip="Your Remain Download Size-Today" CssClass="lblTitleInStatistics"></asp:Label>
                                <asp:Label ID="lblDownload_Size_By_UserID_Today_Remain" runat="server" Text="<%# Download_Size_By_UserID_Today_Remain() %>"
                                    CssClass="lblCountInStatistics"></asp:Label>
                            </div>
                        </ContentTemplate>
                    </asp:UpdatePanel>

此计时器每10秒钟一次,并导致回调 - &gt;由于该计时器,每10秒钟内页面的第一页_加载和主页的最后一页加载! 如何识别主页面和内容页面中的 timer'callback ,并防止因回调而一次又一次地运行代码?

提前致谢

1 个答案:

答案 0 :(得分:2)

您有几种选择:

  1. 删除计时器。看来你实际上并不想要它/需要它。
  2. 在Page_Load上,Master和Page,将您的代码包含在if(!IsPostBack){//code}
  3. 在Page_Load上查看Request.Params["__EVENTTARGET"],看看它是否包含Timer1