更新标签值而不刷新页面

时间:2014-08-09 07:14:44

标签: c# asp.net timer

.aspx代码

 <asp:Timer ID="timer" Interval="3000" runat="server" OnTick="Timer1_Tick">
    </asp:Timer>
    <asp:UpdatePanel ID="UpdatePanel3" runat="server" >
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />
        </Triggers>
        <ContentTemplate>
            <div class="row-fluid">
                <ul class="ov_boxes">
                    <li class="blue">
                        <div class="p_bar_up">
                            <span style="display: none;">2,4,9,7,12,8,16</span>
                        </div>
                        <div class="ov_text">
                            Total <strong><a href="../Reports/GetAllEmployeeReport.aspx" target="_blank">
                                <asp:Label Style="color: White;" ID="lblTotal" runat="server"></asp:Label></a>
                            </strong>
                            <% Response.Write(ToDateString(System.DateTime.Now.ToString())); %>
                        </div>
                    </li>
                </ul>
                <ul class="ov_boxes">
                    <li class="pink">
                        <div class="p_bar_down">
                            <span style="display: none;">20,15,18,14,10,13,9,7</span>
                        </div>
                        <div class="ov_text">
                            Total IN <strong><a href="../Reports/InEmployeeReport.aspx" target="_blank">
                                <asp:Label Style="color: White;" ID="lblInEmp" runat="server"></asp:Label></a>
                            </strong>
                            <% Response.Write(ToDateString(System.DateTime.Now.ToString())); %>
                        </div>
                    </li>
                </ul>
                <ul class="ov_boxes">
                    <li class="grey">
                        <div class="p_line_up">
                        </div>
                        <div class="ov_text">
                            Total OUT<strong><a href="../Reports/OutEmployeeReport.aspx" target="_blank">
                                <asp:Label Style="color: White;" ID="lblTotal" runat="server"></asp:Label></a>
                            </strong>
                            <% Response.Write(ToDateString(System.DateTime.Now.ToString())); %>
                        </div>
                    </li>
                </ul>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel> 

后面的代码是

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        GetTodayInOutData();
        UpdatePanel3.Update();
    }
    private void GetTodayInOutData()
    {
        try
        {
            var objEmployeeBAL = new EmployeeBAL();
            Entity.Employee objEmployee = new Entity.Employee();
            DataSet ds = objEmployeeBAL.GetRealtimeData();
            DataSet ds1 = objEmployeeBAL.GetRealtimeOutData();
            DataSet ds2 = objEmployeeBAL.GetRealtimeInData();
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblTotal.Text = ds.Tables[0].Rows[0]["Total"].ToString();
            }

        }
        catch (Exception)
        {
            throw;
        }
    } 
每隔3秒调用

GetTodayInOutData()方法并获取正确的值,但lblTotal显示旧值意味着它不会更新。我找不到这个错误。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

为标签AutoPostBack设置lblTotal为true,并将UpdateMode属性设置为Conditional

<asp:Label Style="color: White;" AutoPostBack="true" UpdateMode="Conditional" ID="lblTotal" runat="server"></asp:Label>