子ascx页面上带有AsyncPostBackTrigger的Asp Timer会导致Default.aspx重新加载

时间:2013-11-04 16:48:51

标签: asp.net timer ascx

我想要完成的是仅在子ascx控件上重新加载部分页面。但是,如果我在Page Load上对default.aspx放置一个中断,那么中断将会打到计时器滴答。我的理想情况是default.aspx永远不会重新加载。

Default.aspx的

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
        <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
    </Scripts>
</telerik:RadScriptManager>
<script type="text/javascript">
    //Put your JavaScript code here.
</script>
<div>
    <asp:Label runat="server" ID="DynLabel"></asp:Label>
    <asp:PlaceHolder ID="PlaceHolderDyn" runat="server"></asp:PlaceHolder>
</div>
</form>

Default.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Guid g;
        g = Guid.NewGuid();
        DynLabel.Text = g.ToString();
        PlaceHolderDyn.Controls.Add(LoadControl("~/Docks/DynControl.ascx"));


    }


}

DynControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DynControl.ascx.cs" Inherits="Docks_DynControl" %>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
    <telerik:AjaxSetting AjaxControlID="Timer1">
        <UpdatedControls>
            <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="LoadingPanel1">
            </telerik:AjaxUpdatedControl>
        </UpdatedControls>
    </telerik:AjaxSetting>
    <telerik:AjaxSetting AjaxControlID="DropDownList1">
        <UpdatedControls>
            <telerik:AjaxUpdatedControl ControlID="Panel1"></telerik:AjaxUpdatedControl>
            <telerik:AjaxUpdatedControl ControlID="Panel2"></telerik:AjaxUpdatedControl>
        </UpdatedControls>
    </telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="LoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
<asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick"></asp:Timer>
<asp:UpdatePanel ID="Panel1" runat="server" UpdateMode="Conditional" ViewStateMode="Enabled">
<ContentTemplate>
    <asp:PlaceHolder ID="DynamicPlaceHolder" runat="server"></asp:PlaceHolder>
    <asp:Labelrunat="server" ID="DynamiclabelAscx"/>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>

DynControl.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class Docks_DynControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     public void Timer1_Tick(object sender, EventArgs e)
    {

        Guid g;
        g = Guid.NewGuid();
        DynamiclabelAscx.Text = g.ToString();


    }


}

所以基本上Default.aspx上的标签是静态的,只加载一次,定时器将重新加载ascx上的标签,它将始终改变。这是有效的,但是如何防止default.aspx在计时器上重新加载?

谢谢!

2 个答案:

答案 0 :(得分:0)

在做了一些研究之后,这是你问题的答案:

  

异步回发的行为很像同步回发。所有   发生服务器页面生命周期事件,并查看状态和表单数据   保存完好。但是,在渲染阶段,只有内容   UpdatePanel控件被发送到浏览器。页面的其余部分   保持不变。

MSDN Source

所以在Page_Load中包装Default.aspx !IsPostBack方法似乎是要走的路:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Guid g;
        g = Guid.NewGuid();
        DynLabel.Text = g.ToString();
        PlaceHolderDyn.Controls.Add(LoadControl("~/Docks/DynControl.ascx"));
    }
}

答案 1 :(得分:0)

感谢Scott的扩展对话。

我想出来防止回发的最好方法是将所有内容包含在指向该aspx的IFrame中的附加控件aspx / ascx中。这将导致该页面仅呈现任何回发。