我的代码如下:
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick1">
        </asp:Timer>
       </ContentTemplate>
        <Triggers>
       <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
        
        </asp:UpdatePanel>
 cs中代码如下:
 protected void Timer1_Tick1(object sender, EventArgs e)
    {
        LabelTime.Text = DateTime.Now.ToShortDateString() + "\n" + DateTime.Now.ToLongTimeString();
    }
但是结果时间没有变化,只有我单机某个按钮时才有变化。
怎么解决谢谢!

解决方案 »

  1.   

    把其他updatePanel的UpdateMode也设置成"Conditional"
      

  2.   

    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick1"> 
            </asp:Timer> 
    这个放updatepanel外面
      

  3.   

    <%@ Page Language="C#" %><script runat="server">
        protected void Timer1_Tick1(object sender, EventArgs e)
        {
            LabelTime.Text = DateTime.Now.ToShortDateString() + "\n" + DateTime.Now.ToLongTimeString();
            UpdatePanel2.Update();
        }
    </script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick1">
                </asp:Timer>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="LabelTime" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
      

  4.   

    回复4楼:
     根据您的解答我的问题已经解决了谢谢!但是
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>

    UpdatePanel2.Update();
    是什么意思我还不懂,要是能解释一下就好了!谢谢……
      

  5.   


    你把UpdatePanel2.Update();这句去掉照样可以,不信你试试看。最开始你的问题是没把<label>控件放到UpdatePanel里面,所以更新了以后也不能实时的反映到网页上。
      

  6.   

    <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> 
    </Triggers> 这句话意思是说,timer1可以触发这个updatepanel的异步刷新你的关键问题是label没放到updatepanel里边,这就意味着,你告诉服务器你这个label不需要局部刷新,而是整体刷新。所以页面当然就整体刷新了
      

  7.   


    <Triggers />是什么意思我也不懂,我只是照抄你的代码(呵呵!)。实际上,这个是根本没有必要的。自从我发现<Triggers>在masterpage中有bug,我从来都不使用Triggers。我唯一要告诉你的就是直截了当地写上UpdatePanel2.Update()。当然我开始说我不懂是玩笑,但是你的Triiger写错了。对比地,你可以看看代码:<%@ Page Language="C#" %><script runat="server">
        protected void Timer1_Tick1(object sender, EventArgs e)
        {
            LabelTime.Text = DateTime.Now.ToShortDateString() + "\n" + DateTime.Now.ToLongTimeString();
        }
    </script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick1">
                </asp:Timer>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="LabelTime" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
    当你把Timer放到UpdatePanel内,它总是触发自己所嵌入的所有UpdatePanel,从而不需要Update()这些UpdataPanel。但是要让别的区域刷新,需要写一行代码或者使用Trigger设置。
      

  8.   

    你要么写Update()方法,要么使用<Trigger>声明(但是要放对地方),二者选一。由于后者有bug,我总是使用前者。
      

  9.   

    或者再把对比写得更全面一点:<%@ Page Language="C#" %><script runat="server">
        protected void Timer1_Tick1(object sender, EventArgs e)
        {
            LabelTime.Text = DateTime.Now.ToShortDateString() + "\n" + DateTime.Now.ToLongTimeString();
            Label2.Text = Label1.Text = LabelTime.Text;
            UpdatePanel3.Update();
        }
    </script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick1">
                </asp:Timer>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="LabelTime" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
      

  10.   

    谢谢各位!尤其是8、9、10楼。
    我已经知道错在哪了。
    UpdateMode="Conditional" 和 UpdateMode="Always" 的区别是什么啊?
      

  11.   


    Conditional:这个是满足条件才更新
    Always:这个是无条件的更新。也就是每次都会更新。如果当一个页面有多个updatepanel时,即使其他的updatepanel更新,也会导致这个updatepanel更新所以才会出现楼主现在的问题。timer刷新时会刷新整个页面