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

分享于 

3分钟阅读

Web开发

  繁體

Sample Image - GoogleCalendarEventLink.gif

介绍

Google最近发布了日历应用程序。 如果你的网站有事件信息,允许用户使用谷歌日历事件链接将该事件添加到自己的Google日历。

示例链接

我已经创建了一个新的ASP.NET HyperLink 控件,并添加了一些属性,允许将特定信息附加到 HyperLink 控件。 然后,我提供了覆盖的Render() 函数,它使用提供的信息组装特定的NavigateUrl,如有必要。

Render() 代码

protectedoverridevoid Render(HtmlTextWriter writer)
{
 StringBuilder url = new StringBuilder();
 url.Append("http://www.google.com/calendar/event?");
 url.Append("action=TEMPLATE");
 // Event titlestring 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 eventconststring UTCFORMATSTRING = "yyyyMMddTHHmmssZ";
 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 infoif (!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 locationif (!string.IsNullOrEmpty(this.EventLocation))
 {
 url.AppendFormat("&location={0}", 
 HttpUtility.UrlEncode(this.EventLocation));
 }
 // Event descriptionif (!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 longif (this.MarkAsBusy)
 {
 url.AppendFormat("&trp={0}", this.MarkAsBusy);
 }
 // Set the NavigateUrlthis.NavigateUrl = url.ToString();
 base.Render(writer);
 }
}

这是我第一个在服务器控制( 我通常只写用户控件),所以让我知道如果存在问题,但请轻松:

另请参阅


相关文章