Arpit gupta

Arpit gupta

  • NA
  • 5
  • 19.8k

Problem using timer control in datalist

Aug 7 2010 8:23 AM
Hi all

I have used timer control in datalist in my web application . i bind all fields as below

 <asp:DataList ID="PrdDataList" runat="server" RepeatDirection="Horizontal"
                    RepeatColumns="5" Width="656px" DataKeyField="id"
                    onitemdatabound="PrdDataList_ItemDataBound">
                    <ItemTemplate>
                        <table border="0" cellspacing="0" cellpadding="0" class="box2">
                            <tr>
                                <td class="hd">
                                    <span>Auction</span><asp:label ID="lblid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "id")%>'></asp:label>
                                </td>
                            </tr>                                                     
                              <tr>
                                    <td>
                                            <asp:Label ID="lblTimeWatch" runat="server"
                                                    style="color:#68b121; font-weight: 700;      font-size: 26px" Text='<%# DataBinder.Eval(Container.DataItem, "TimeDiff")%>'>  </asp:Label>
                 
                          <asp:Timer ID="BidTimer" runat="server"  ontick="BidTimer_Tick">
                                                    </asp:Timer>
                                           <input id="mseconds" runat="server" type="hidden" />
                                           <input id="second" runat="server" type="hidden" />
                                           <input id="minute" type="hidden"  runat="server" />
                                           <input id="hour" type="hidden"  runat="server" />
                                             <input id="day" type="hidden" runat="server" />
                                              
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                                    <ContentTemplate>
                                               <h1><img alt="rs" src="image/canvas.PNG" />&nbsp;
                                                      <asp:Label ID="lblbidprice" Text="0.50" runat="server" ></asp:Label></h1>
                                                    </ContentTemplate>
                                            </asp:UpdatePanel>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <b>Buy Now:</b>
                                            <span><img alt="rs" src="image/canvas.PNG" /></span>
                                             <%#DataBinder.Eval(Container.DataItem,"Price") %>
                                        </td>

                                    </tr>
                                    <tr>
                                        <td>
                                            <table>
                                                <tr>
                                                    <td class="gray" id="tdbidder" runat="server" style="padding-left:20px">
                                                        <asp:DataList ID="dlbidder" runat="server"  GridLines="None">
                                                             <ItemTemplate>
                                                                <%#Eval("UserId") %>
                                                            </ItemTemplate>                                                               
                                                        </asp:DataList>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td >
                                            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                                                <ContentTemplate>        
                                                    <asp:ImageButton ID="btnBid" runat="server" CommandName="BidClick"
                                                        ImageUrl="~/images/btn_bid.jpg" onclick="btnBid_Click" ></asp:ImageButton>        
                                                    <%--<asp:Button id="btnBid" style="background-color:#33CC33"  Text="Bid" runat="server" onclick="btnBid_Click" /> --%>
                                                </ContentTemplate>
                                            </asp:UpdatePanel>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>

                        </table>
                    </ItemTemplate>
                </asp:DataList>                      



I bind the data list at page load. But the timer start only with the last template created. The 'timeDiff" is exact difference between timer starttime and currentdatetime. The code is here:



        protected void BidTimer_Tick(object sender, EventArgs e)
        {
            PerformClock();
        }
         void PerformClock()
        {
            foreach (DataListItem dlitem in PrdDataList.Items)
            {
                lblid = (Label)dlitem.FindControl("lblid");
                auc_id = Convert.ToInt32(lblid.Text);
                BidTimer = (Timer)dlitem.FindControl("BidTimer");
                mseconds = (HtmlInputHidden)dlitem.FindControl("mseconds");
                second = (HtmlInputHidden)dlitem.FindControl("second");
                minute = (HtmlInputHidden)dlitem.FindControl("minute");
                hour = (HtmlInputHidden)dlitem.FindControl("hour");
                day = (HtmlInputHidden)dlitem.FindControl("day");
                lblTimeWatch = (Label)dlitem.FindControl("lblTimeWatch");
                btnBid = (ImageButton)dlitem.FindControl("btnBid"); 
            }

            MilliSeconds = Convert.ToInt32(mseconds.Value);
            Seconds = Convert.ToInt32(second.Value);
            Minutes = Convert.ToInt32(minute.Value);
            Hours = Convert.ToInt32(hour.Value);
            Days = Convert.ToInt32(day.Value);
            ds = AuctionDAL.retrieveAll();

            if (lblTimeWatch.Text == "01.00:01:00")
            {
                lblTimeWatch.Text = "01.00:00:59:";
                Minutes = 0;
                Hours = 0;
                Seconds = 59;
                Days = 1;
            }
            else if (lblTimeWatch.Text == "01.01:01:00")
            {
                lblTimeWatch.Text = "01.01:00:59";
                Minutes = 0;
                Hours = 1;
                Seconds = 59;
                Days = 1;

            }
            else if (lblTimeWatch.Text == "01.01:00:00")
            {
                lblTimeWatch.Text = "01.00:59:59";
                Days = 1;
                Hours = 0;
                Minutes = 59;
                Seconds = 59;
            }
            else if (lblTimeWatch.Text == "01.00:00:00")
            {
                lblTimeWatch.Text = "00.59:59:59";
                Days = 0;
                Hours = 59;
                Minutes = 59;
                Seconds = 59;
            }


            else if (lblTimeWatch.Text == "00:00:00")
            {
                DataSet dsbid = new DataSet();
                dsbid = BidDAL.retrieveBidData(auc_id);
                if (dsbid.Tables[0].Rows.Count > 0)
                {

                    Seconds--;
                    if (Seconds == -1)
                    {
                        lblTimeWatch.Text = "Go1";
                    }

                }
                else
                {

                    lblTimeWatch.Text = "00:00:15";
                    Seconds = 15;
                    Minutes = 0;
                    Hours = 0;
                    MilliSeconds = 0;

                }



            }
            else if (lblTimeWatch.Text == "Go1")
            {
                Seconds--;
                if (Seconds == -2)
                {
                    lblTimeWatch.Text = "Go2";
                }

            }
            else if (lblTimeWatch.Text == "Go2")
            {
                Seconds--;
                if (Seconds == -3)
                {
                    lblTimeWatch.Text = "Go3";
                }
            }
            else if (lblTimeWatch.Text == "Go3")
            {
                Seconds--;
                if (Seconds == -4)
                {
                    BidTimer.Enabled = false;
                    lblTimeWatch.Text = "Sold";

                    //btnBid.Text= "BidEnd";
                    btnBid.Enabled = false;
                }
            }
            else if (lblTimeWatch.Text == "00:00:15")
            {
                Seconds--;
                Minutes = 0;
                Hours = 0;
                MilliSeconds = 0;

            }
            else if (lblTimeWatch.Text == "00:01:00")
            {
                Minutes = 0;
                MilliSeconds = 0;
                Seconds = 59;
                Hours = 0;
            }

            else
            {
                if (Seconds % 60 == 0)
                {
                    Minutes--;
                    MilliSeconds = 0;
                    Seconds = 59;


                    if (Minutes % 3600 == 0)
                    {
                        Hours--;
                        MilliSeconds = 0;
                        Seconds = 59;
                        Minutes = 59;

                        if (Hours % 86400 == 0)
                        {
                            Days--;
                            Hours = 23;
                            MilliSeconds = 0;
                            Seconds = 59;
                            Minutes = 59;

                        }
                    }
                }
                Seconds--;


            }


            mseconds.Value = MilliSeconds.ToString();
            second.Value = Seconds.ToString();
            minute.Value = Minutes.ToString();
            hour.Value = Hours.ToString();
            day.Value = Days.ToString();

            if (Days > 0)
            {
                lblTimeWatch.Text = Days.ToString("00") + "." + Hours.ToString("00") + ":" + Minutes.ToString("00") + ":" + Seconds.ToString("00");
            }
            else
            {
                if (Seconds >= 0)
                {
                    lblTimeWatch.Text = Hours.ToString("00") + ":" + Minutes.ToString("00") + ":" + Seconds.ToString("00");
                }
            }


        }


Please help me so that i can successfully use timer in my web application.


for like website click here http://www.BidRivals.com