SharePoint AJAX in Webparts

Recently in one of my projects I needed to show the date and time along with time zone in abbreviated form based on the Regional Settings set by the user.
The output needed to be in this form
The following code helps achieve this. Use of AJAX update panel makes the webpart to show updated time without refreshing the page.
The reference to System.Web.Extensions needs to be added in order to use AJAX. See here for reference.
public class DateTimeTickerWebpart : WebPart
{      
    UpdatePanel updatePanel;
    Literal ltZone;
    Literal ltDateTime;
    Timer timer;
    string zoneName = string.Empty;

    /// <summary>
    ///  Creates controls on the page.
    /// </summary>
    protected override void CreateChildControls()
    {
        updatePanel = new UpdatePanel();
        ltZone = new Literal();
        ltDateTime = new Literal();
        updatePanel.ID = "updatePanel";
        timer = new Timer()
        {
            ID = "timer",
            Interval = 60000,
        };
        timer.Tick += new EventHandler<EventArgs>(timer_Tick);
        this.Controls.Add(timer);
        updatePanel.Triggers.Add(new AsyncPostBackTrigger()
        {
            ControlID = timer.ID,
            EventName = "Tick"
        }
        );
        this.Controls.Add(ltDateTime);
        this.Controls.Add(ltZone);
        this.Controls.Add(updatePanel);          
    }

    void timer_Tick(object sender, EventArgs e)
    {

    }

    protected override void OnPreRender(EventArgs e)
    {
        GetTimeAndZone();
    }

    private void GetTimeAndZone()
    {
        string zoneStandardName = string.Empty;
        SPRegionalSettings regSettings = SPContext.Current.Web.CurrentUser.RegionalSettings;
        if (regSettings != null)
        {
            zoneName = regSettings.TimeZone.Description;
        }
        else
        {
            zoneName = SPContext.Current.Web.RegionalSettings.TimeZone.Description;
        }

        try
        {
            ReadOnlyCollection<TimeZoneInfo> availableZones = TimeZoneInfo.GetSystemTimeZones();
            foreach (TimeZoneInfo zone in availableZones)
            {
                string zoneDisplayName = zone.DisplayName;
                if (zoneDisplayName.Contains("&"))
                    zoneDisplayName = zone.DisplayName.Replace("&", "and");
                if (zoneDisplayName.Equals(zoneName, StringComparison.CurrentCultureIgnoreCase))
                {
                    zoneStandardName = zone.StandardName;
                    char[] delim = { ' ' };
                    string[] words = zone.Id.Split(delim, StringSplitOptions.RemoveEmptyEntries);
                    string abbrev = string.Empty;
                    if (words.Length > 1)
                    {
                        foreach (string chaStr in words)
                        {
                            abbrev += chaStr[0];
                        }
                    }
                    else
                    {
                        abbrev = words[0];
                    }

                    // Place a space between datetime and zone parts
                    ltZone.Text = " " + abbrev;
                    zoneName = " " + abbrev;
                    break;
                }
            }

            // Get current UTC date and convert it to Users selected time zone.
            DateTime timeUtc = DateTime.UtcNow;
            if (!zoneStandardName.Equals("Coordinated Universal Time", StringComparison.CurrentCultureIgnoreCase))
            {
                TimeZoneInfo userSelectedZone = TimeZoneInfo.FindSystemTimeZoneById(zoneStandardName);
                DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, userSelectedZone);
                ltDateTime.Text = userTime.ToString("dd MMMM yyyy HH:mm");
            }
            else
            {
                ltDateTime.Text = timeUtc.ToString("dd MMMM yyyy HH:mm");
            }

            updatePanel.ContentTemplateContainer.Controls.Add(ltDateTime);
            updatePanel.ContentTemplateContainer.Controls.Add(ltZone);

        }
        catch
        {
            // Do nothing.
        }

    }
}
profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+