AJAX asp:UpdatePanel与asp:Timer一起使用时永远不会刷新。

huangapple go评论57阅读模式
英文:

AJAX asp:UpdatePanel with a asp:Timer never refreshes

问题

我想我会尝试使用计时器和更新面板进行实验。我有一个简单的页面,上面放了一个更新面板控件、一个标签和一个计时器。

"TICK" 代码会使用当前时间更新标签。
面板从未刷新。如果我将计时器对象放在更新面板之外,整个页面每隔1秒刷新一次。所以这告诉我AJAX实际上是在工作的。只是不仅仅是更新面板,而是更新面板中的时间标签。我尝试过带有触发器标签和不带触发器标签的方式。没有变化。我在Timer1_Tick子代码中放了一个 "STOP" 指令,当我检查lbl1.text值时,它实际上已经有了更新后的值,但它就是不会在更新面板中呈现出来。这就是为什么这是个谜。在试了几个小时的各种方法后,我想我应该向专家们请教。

ASPX对象:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="updPnl1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>
        <asp:Label ID="lbl1" runat="server" Text="这是我的标签" />
    </ContentTemplate>
</asp:UpdatePanel>

后台代码(我尝试过带有和不带有 updPnl1.Update 语句),正如我所说,如果我停止代码,lbl1.text的值实际上已经更新了,但不会以此方式显示出来:

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    lbl1.Text = "UpdatePanel1 在:" & DateTime.Now.ToLongTimeString() & " 刷新"
    '停止
    updPnl1.Update()
End Sub
英文:

I thought I would experiment with a timer and update panel. I have a simple page that I put an updatepanel control, a label, and a timer.

The "TICK" code updates the label with the current time.
The panel never refreshes. If I put the timer object outside the updatepanel, the whole page refreshes every 1 second. So that tells me that AJAX is in fact working. Just never for the update panel only with the time tag inside the updatepanel. I've tried it with Triggers tags and without. No change. I put a "STOP" directive in the Timer1_Tick sub/code, and when I inspect the value of the lbl1.text value, it does in fact have the updated value, but it just won't render it in the updatepanel. This is why it's a mystery. After several hours of trying anything I could find, I thought I would just ask the experts.

ASPX objects:

&lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&gt;
&lt;/asp:ScriptManager&gt;

        &lt;asp:UpdatePanel ID=&quot;updPnl1&quot; runat=&quot;server&quot; UpdateMode=&quot;Conditional&quot;&gt;
            &lt;Triggers&gt;
                &lt;asp:AsyncPostBackTrigger ControlID=&quot;Timer1&quot; EventName=&quot;Tick&quot; /&gt;
            &lt;/Triggers&gt;
            &lt;ContentTemplate&gt;
                &lt;asp:Timer ID=&quot;Timer1&quot; runat=&quot;server&quot; OnTick=&quot;Timer1_Tick&quot; Interval=&quot;1000&quot;&gt;&lt;/asp:Timer&gt;
                &lt;asp:Label ID=&quot;lbl1&quot; runat=&quot;server&quot; Text=&quot;THIS IS MY LABEL&quot;  /&gt;
            &lt;/ContentTemplate&gt;
            
        &lt;/asp:UpdatePanel&gt;

Code behind (I tried with and without the updPnl1.Update statement), and like I said, if I STOP the code, the value of lbl1.text is actually updated, but no displaying it as such):

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    lbl1.Text = &quot;UpdatePanel1 refreshed at: &quot; &amp; DateTime.Now.ToLongTimeString()
    &#39;Stop    
    updPnl1.Update()
End Sub

I tried various examples I've found. I was expecting the lbl1.text to update with the current time.

答案1

得分: 1

已解决!
从中可以得出以下结论。
在页面上使用 response.write 时,计时器将对整个页面起作用,但在 UPDATEPANEL 中的计时器根本不会渲染,尽管后台代码将为更新面板中的计时器正常运行。
感谢 Albert 的帮助。

英文:

SOLVED!
The takeaway from this.
When having a response.write on the page, a timer will work for the entire page but a timer in an UPDATEPANEL will not render at all, although the code behind will function for the timer in the update panel.
Thanks Albert for the helps.

答案2

得分: 0

以下是您要翻译的内容:

"Really, in 99% of cases, you don't need nor want a trigger. Why?

Any button, or anything inside of the update panel that cases a post-back works like a regular page lifecycle.

So, if you want the UP to update say due to a button click? Then just make sure the button is inside of the UP and you are 100% fine. (no triggers required).

So, say we want a count down clock of some type.

So, in the update panel, drop in your text boxes (to display the time). Add a button to start, and even one to stop the timer.

And MAKE sure you drop the timer inside of the update panel.

Remember, you can think of a Update Panel as a self contained web page inside of the current page.

Keep in mind while a update panel LOOKS like there is not a post-back, there is! In fact even the page's "load" event fires and triggers each time!

The correct term here is what we call a "partial" page post-back. So, the full page life cycle occurs here, but ONLY for the things inside of the Update Panel.

So, let's drop in three text boxes and a start and stop button inside of a update panel.

Say this:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

        <div style="float: left">
            <asp:Label ID="lblHours" runat="server" Text="Hours" Font-Size="Larger"></asp:Label>
            <br />
            <asp:TextBox ID="txtHours" runat="server" Text="0" CssClass="tbox"></asp:TextBox>
        </div>

        <div style="float: left; margin-left: 20px">
            <asp:Label ID="lblMin" runat="server" Text="Minutes" Font-Size="Larger"></asp:Label>
            <br />
            <asp:TextBox ID="txtMin" runat="server" Text="0" CssClass="tbox"></asp:TextBox>
        </div>

        <div style="float: left; margin-left: 15px">
            <asp:Label ID="lblsec" runat="server" Text="Seconds" Font-Size="Larger"></asp:Label>
            <br />
            <asp:TextBox ID="txtSec" runat="server" BorderStyle="Solid" CssClass="tbox"></asp:TextBox>
        </div>

        <div style="clear: both; height: 15px"></div>
        <asp:Button ID="cmdStart" runat="server" Text="Start" CssClass="btn btn-info" />
        <asp:Button ID="cmdStop" runat="server" Text="Stop" CssClass="btn btn-danger" Style="margin-left: 20px" />

        <br />
        <asp:Label ID="lblDone" runat="server"></asp:Label>
        <asp:Timer ID="Timer1" runat="server" Enabled="False"></asp:Timer>

    </ContentTemplate>
</asp:UpdatePanel>

Note how we dropped the timer control inside of the panel. A timer control is actually like a button click - it will "post back" the page based on the interval - it really not much different then dropping a button on the page and clicking it.

Note how I have the timer as disabled to start out.

So, now the user can enter values in to the text boxes, and hit the start button.

So, the start stop code (code behind) is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        txtSec.Text = "5" ' setup a starting value
    End If

End Sub

Protected Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click

    ' take entered values - convert to time
    Dim MyTime As DateTime
    MyTime = TimeSerial(txtHours.Text, txtMin.Text, txtSec.Text)

    ViewState("MyTime") = MyTime   ' save our time
    Timer1.Interval = 1000  ' update every one second
    Timer1.Enabled = True   ' start the timer.
    lblDone.Text = "Started"

    cmdStart.Enabled = False  ' disable our start button

End Sub

Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Dim myTime As DateTime = ViewState("MyTime")

    ' check if we are to stop
    If myTime <= TimeSerial(0, 0, 0) Then
        Timer1.Enabled = False      ' turn off timer
        ' do whatever for end of timer
        lblDone.Text = "Done!"
        cmdStart.Enabled = True
        Exit Sub
    End If

    Dim OneSecond As TimeSpan = TimeSpan.FromSeconds(1)

    myTime = myTime - OneSecond
    ' update text boxes
    txtHours.Text = myTime.Hour
    txtMin.Text = myTime.Minute
    txtSec.Text = myTime.Second
    ViewState("MyTime") = myTime

End Sub

Protected Sub cmdStop_Click(sender As Object, e As EventArgs) Handles cmdStop.Click
    Timer1.Enabled = False
    cmdStart.Enabled = True
End Sub

Edit

Here is the style I used for the three boxes. You can drop in this style for the boxes right after the form tag.

        <style>
            .tbox
               {font:x-large bold;border-style:solid;
                height:34px;width:37px;
                text-align:center;
               }
        </style>

So, the result now looks like this:"

英文:

Really, in 99% of cases, you don't need nor want a trigger. Why?

Any button, or anything inside of the update panel that cases a post-back works like a regular page lifecycle.

So, if you want the UP to update say due to a button click? Then just make sure the button is inside of the UP and you are 100% fine. (no triggers required).

So, say we want a count down clock of some type.

So, in the update panel, drop in your text boxes (to display the time). Add a button to start, and even one to stop the timer.

And MAKE sure you drop the timer inside of the update panel.

Remember, you can think of a Update Panel as a self contained web page inside of the current page.

Keep in mind while a update panel LOOKS like there is not a post-back, there is! In fact even the page's "load" event fires and triggers each time!

The correct term here is what we call a "partial" page post-back. So, the full page life cycle occurs here, but ONLY for the things inside of the Update Panel.

So, let's drop in three text boxes and a start and stop button inside of a update panel.

Say this:

    &lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&gt;&lt;/asp:ScriptManager&gt;

    &lt;div style=&quot;padding: 20px&quot;&gt;

        &lt;asp:UpdatePanel ID=&quot;UpdatePanel1&quot; runat=&quot;server&quot;&gt;
            &lt;ContentTemplate&gt;

                &lt;div style=&quot;float: left&quot;&gt;
                    &lt;asp:Label ID=&quot;lblHours&quot; runat=&quot;server&quot; Text=&quot;Hours&quot; Font-Size=&quot;Larger&quot;&gt;&lt;/asp:Label&gt;
                    &lt;br /&gt;
                    &lt;asp:TextBox ID=&quot;txtHours&quot; runat=&quot;server&quot; Text=&quot;0&quot; CssClass=&quot;tbox&quot;&gt;&lt;/asp:TextBox&gt;
                &lt;/div&gt;

                &lt;div style=&quot;float: left; margin-left: 20px&quot;&gt;
                    &lt;asp:Label ID=&quot;lblMin&quot; runat=&quot;server&quot; Text=&quot;Minutes&quot; Font-Size=&quot;Larger&quot;&gt;&lt;/asp:Label&gt;
                    &lt;br /&gt;
                    &lt;asp:TextBox ID=&quot;txtMin&quot; runat=&quot;server&quot; Text=&quot;0&quot; CssClass=&quot;tbox&quot;&gt;&lt;/asp:TextBox&gt;
                &lt;/div&gt;

                &lt;div style=&quot;float: left; margin-left: 15px&quot;&gt;
                    &lt;asp:Label ID=&quot;lblsec&quot; runat=&quot;server&quot; Text=&quot;Seconds&quot; Font-Size=&quot;Larger&quot;&gt;&lt;/asp:Label&gt;
                    &lt;br /&gt;
                    &lt;asp:TextBox ID=&quot;txtSec&quot; runat=&quot;server&quot; BorderStyle=&quot;Solid&quot; CssClass=&quot;tbox&quot;&gt;&lt;/asp:TextBox&gt;
                &lt;/div&gt;

                &lt;div style=&quot;clear: both; height: 15px&quot;&gt;&lt;/div&gt;
                &lt;asp:Button ID=&quot;cmdStart&quot; runat=&quot;server&quot; Text=&quot;Start&quot; CssClass=&quot;btn btn-info&quot; /&gt;
                &lt;asp:Button ID=&quot;cmdStop&quot; runat=&quot;server&quot; Text=&quot;Stop&quot; CssClass=&quot;btn btn-danger&quot; Style=&quot;margin-left: 20px&quot; /&gt;

                &lt;br /&gt;
                &lt;asp:Label ID=&quot;lblDone&quot; runat=&quot;server&quot;&gt;&lt;/asp:Label&gt;
                &lt;asp:Timer ID=&quot;Timer1&quot; runat=&quot;server&quot; Enabled=&quot;False&quot;&gt;&lt;/asp:Timer&gt;

            &lt;/ContentTemplate&gt;
        &lt;/asp:UpdatePanel&gt;

    &lt;/div&gt;

Note how we dropped the timer control inside of the panel. A timer control is actually like a button click - it will "post back" the page based on the interval - it really not much different then dropping a button on the page and clicking it.

Note how I have the timer as disabled to start out.

So, now the user can enter values in to the text boxes, and hit the start button.

So, the start stop code (code behind) is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        txtSec.Text = &quot;5&quot; &#39; setup a starting value
    End If

End Sub

Protected Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click

    &#39; take entered values - convert to time
    Dim MyTime As DateTime
    MyTime = TimeSerial(txtHours.Text, txtMin.Text, txtSec.Text)

    ViewState(&quot;MyTime&quot;) = MyTime   &#39; save our time
    Timer1.Interval = 1000  &#39; upddate every one second
    Timer1.Enabled = True   &#39; start the timer.
    lblDone.Text = &quot;Started&quot;

    cmdStart.Enabled = False  &#39; disable our start button

End Sub

Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Dim myTime As DateTime = ViewState(&quot;MyTime&quot;)

    &#39; check if we are to stop
    If myTime &lt;= TimeSerial(0, 0, 0) Then
        Timer1.Enabled = False      &#39; turn off timer
        &#39; do whatever for end of timer
        lblDone.Text = &quot;Done!&quot;
        cmdStart.Enabled = True
        Exit Sub
    End If

    Dim OneSecond As TimeSpan = TimeSpan.FromSeconds(1)

    myTime = myTime - OneSecond
    &#39; update text boxes
    txtHours.Text = myTime.Hour
    txtMin.Text = myTime.Minute
    txtSec.Text = myTime.Second
    ViewState(&quot;MyTime&quot;) = myTime

End Sub

Protected Sub cmdStop_Click(sender As Object, e As EventArgs) Handles cmdStop.Click
    Timer1.Enabled = False
    cmdStart.Enabled = True
End Sub

Edit

Here is the style I used for the three boxes. You can drop in this style for the boxes right after the form tag.

        &lt;style&gt;
            .tbox
               {font:x-large bold;border-style:solid;
                height:34px;width:37px;
                text-align:center;
               }
        &lt;/style&gt;

So, the result now looks like this:

AJAX asp:UpdatePanel与asp:Timer一起使用时永远不会刷新。

Edit 2: Example working code based on question

So the posted markup is this:

    &lt;asp:UpdatePanel ID=&quot;updPnl1&quot; runat=&quot;server&quot; UpdateMode=&quot;Conditional&quot;&gt;

        &lt;ContentTemplate&gt;

            &lt;asp:Timer ID=&quot;Timer1&quot; runat=&quot;server&quot;
                Interval=&quot;1000&quot;
                Enabled=&quot;true&quot;  &gt;
            &lt;/asp:Timer&gt;

            &lt;asp:Label ID=&quot;lbl1&quot; runat=&quot;server&quot; Text=&quot;THIS IS MY LABEL&quot; /&gt;

        &lt;/ContentTemplate&gt;

    &lt;/asp:UpdatePanel&gt;

And code behind is this:

Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    lbl1.Text = &quot;UpdatePanel1 refreshed at: &quot; &amp; DateTime.Now.ToLongTimeString()

End Sub

So, now we see this:

AJAX asp:UpdatePanel与asp:Timer一起使用时永远不会刷新。

huangapple
  • 本文由 发表于 2023年6月5日 23:52:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定