Prashant16
Prashant16

Reputation: 1526

Update label value without refreshing page

.aspx code

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

and code behind is

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

GetTodayInOutData() method is called after every 3 second and get the correct value but lblTotal displays old value means it's not updating. I can not find what is the wrong in this. Any help would be appreciated. Thanks.

Upvotes: 0

Views: 2757

Answers (1)

Shaharyar
Shaharyar

Reputation: 12439

Set AutoPostBack true for Label lblTotal and also set UpdateMode property to Conditional:

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

Upvotes: 1

Related Questions