Вопрос

I Have a Telerik Radgrid bound to a SQLDatasource (items are databound), in one of the data bound columns there is a value of type DateTime format is H:mm:ss {0:H:mm:ss}. I then added a template column that shows "time left"

So: Start Time - Now = Time Left

I wanted this Ajaxified so I added a timer and a label inside an updatepanel in the template column - for every tick (1000ms) I recalculate the time left.

This seems to work but only for the First Row in the RadGrid

Here are the code snippets

ASP

    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column"     UniqueName="TemplateColumn"
HeaderText="Starting in">
  <ItemTemplate>
     <contenttemplate>               
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
          <asp:Label ID="lbltime" runat="server" Text="00:00:00"></asp:Label>
          <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick"></asp:Timer>
         </ContentTemplate>
         </asp:UpdatePanel>
      </contenttemplate>
  </ItemTemplate>
</telerik:GridTemplateColumn>

C#

        protected void Timer1_Tick(object sender, EventArgs e)
    {
        GridDataItem viewitem = (sender as System.Web.UI.Timer).Parent.Parent.Parent.Parent as GridDataItem;

        DateTime starttime = DateTime.ParseExact((viewitem.FindControl("Strat_TimeLabel") as Label).Text, "H:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
        DateTime nowtime = System.DateTime.Now;
        TimeSpan ts = new TimeSpan();
        ts = starttime - nowtime;

        GridDataItem item = (sender as System.Web.UI.Timer).Parent.Parent.Parent.Parent as GridDataItem;
        if (ts.Seconds >= 0 && ts.Minutes > 0)
        {
            DateTime d = new DateTime(2000, 01, 01, ts.Hours, ts.Minutes, ts.Seconds);
            (item.FindControl("lbltime") as Label).Text = d.ToString("H:mm:ss");
        }
        else
        {
            (item.FindControl("lbltime") as Label).Text = "00:00:00";
        }
    }  

Any help or advice would be appreciated

Это было полезно?

Решение

If anyone is interested I found a work around.

I moved the updatepanel to encapsulate the entire RadGrid (the Timer also is still contained in the updatepanel)

I changed the C# code behind of Timer1_Tick to

        foreach (GridDataItem item in RadGrid1.Items)
        {
            DateTime starttime = DateTime.ParseExact((item.FindControl("Strat_TimeLabel") as Label).Text, "H:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
            DateTime nowtime = System.DateTime.Now;
            TimeSpan ts = new TimeSpan();
            ts = starttime - nowtime;

            System.Web.UI.Timer).Parent.Parent.Parent.Parent as GridDataItem;
            if (ts.Seconds >= 0 && ts.Minutes > 0)
            {
                DateTime d = new DateTime(2000, 01, 01, ts.Hours, ts.Minutes, ts.Seconds);
                (item.FindControl("lbltime") as Label).Text = d.ToString("H:mm:ss");
            }
            else
            {
                (item.FindControl("lbltime") as Label).Text = "Complete";
            }   
        }

Then for every GridCommand I simply Disabled/Enable the timer

    InsertCommand: Timer1.enabled = false;
    EditCommand: Timer1.enabled = false;
    UpdateCommand: Timer1.enabled = True;
    CancelCommand: Timer1.enabled = false;

Probably not the most eloquent way as I would have liked to contain the postback event in the Item Template... but it works... Improvements and suggestions are always welcome.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top