65.9K
CodeProject 正在变化。 阅读更多。
Home

创建 Google 日历事件超链接的 ASP.NET 自定义控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.06/5 (6投票s)

2006年4月13日

CPOL
viewsIcon

118569

downloadIcon

590

一个 ASP.NET 自定义控件,用于创建 Google 日历事件超链接。

Sample Image - GoogleCalendarEventLink.gif

引言

Google 最近发布了 Google 日历 应用程序。如果您的网站有事件信息,请允许用户轻松地将事件添加到他们自己的 Google 日历中,使用 Google 日历事件链接

示例链接

我创建了一个新的 ASP.NET HyperLink 控件,并添加了一些属性,允许将特定信息附加到 HyperLink 控件中,用于 Google 日历事件。然后,我提供了一个重写的 Render() 函数,该函数使用提供的信息组装特定的 NavigateUrl,并在必要时提供默认值。

Render() 代码

protected override void Render(HtmlTextWriter writer)
{
    StringBuilder url = new StringBuilder();
    url.Append("http://www.google.com/calendar/event?");
    url.Append("action=TEMPLATE");

    // Event title

    string eventText = this.EventTitle;
    if (string.IsNullOrEmpty(eventText))
    {
        eventText = this.Text;
    }
    if (!string.IsNullOrEmpty(eventText))
    {
        url.AppendFormat("&text={0}", 
                         HttpUtility.UrlEncode(eventText));
    }

    // Event dates

    // TODO: Validate that a start or end date has been specified

    url.Append("&dates=");
    if (this.StartDateTime != null)
    {
        if (this.AllDayEvent || (this.StartDateTime == this.EndDateTime))
        {
            url.AppendFormat("{0}/{0}", 
                this.StartDateTime.ToString("yyyyMMdd"));
        }
        else
        {
            // TODO: Validate that EndDateTime is set,
            // because this is not an all day event

                const string UTCFORMATSTRING = "yyyyMMdd\\THHmmss\\Z";
                url.AppendFormat("{0}/{1}", 
                  this.StartDateTime.ToUniversalTime().ToString(UTCFORMATSTRING),
                  this.EndDateTime.ToUniversalTime().ToString(UTCFORMATSTRING));
            }
        }

        // TODO: Apparently on sprop is required by google,
        // so validate that one is specified
        // Organization info

        if (!string.IsNullOrEmpty(this.OrganizerName))
        {
            url.AppendFormat("&sprop=name:{0}", 
                HttpUtility.UrlEncode(this.OrganizerName));
        }
        if (!string.IsNullOrEmpty(this.OrganizerWebsite))
        {
            url.AppendFormat("&sprop=website:{0}", 
                HttpUtility.UrlEncode(this.OrganizerWebsite));
        }

        // Event location

        if (!string.IsNullOrEmpty(this.EventLocation))
        {
            url.AppendFormat("&location={0}", 
                HttpUtility.UrlEncode(this.EventLocation));
        }

        // Event description

        if (!string.IsNullOrEmpty(this.EventDescription))
        {
            url.AppendFormat("&details={0}", 
                HttpUtility.UrlEncode(this.EventDescription));
        }

        // Free/Busy
        // Only add to url if true since default false
        // and url could already be really long

        if (this.MarkAsBusy)
        {
            url.AppendFormat("&trp={0}", this.MarkAsBusy);
        }

        // Set the NavigateUrl

        this.NavigateUrl = url.ToString();
        base.Render(writer);
    }
}

这是我第一次尝试编写服务器控件(我通常只编写用户控件),所以如果存在问题请告诉我,但请温柔一些 :)

另请参阅

© . All rights reserved.