how can recognize Timer's callback (Timer Is In Master Page) In both Master & Content Page

人走茶凉 提交于 2019-12-25 04:05:17

问题


my pages are base on master and content pages in asp.net with c#.
i have a timer in master page like below :

                    <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>

this timer ticks every 10 seconds and causes callbacks -> first page_load of content page and last page_load of master page fire every 10 seconds because of that timer!
how can i recognize that timer'callback in both master and content pages and prevent running codes again and again because of callback?

thanks in advance


回答1:


You have several options:

  1. Remove the timer. It seems you don't actually want it/need it.
  2. on Page_Load of both, Master and Page, enclose your code inside a if(!IsPostBack){//code}
  3. On Page_Load check Request.Params["__EVENTTARGET"] and see if it contains Timer1


来源:https://stackoverflow.com/questions/7900874/how-can-recognize-timers-callback-timer-is-in-master-page-in-both-master-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!