2012年2月21日 星期二

Create Google Calendar Event using Google Calendar API


Google api asp.net client download







public void CreateCalendarEvent(string content)
        {
            CalendarService calendarService = new CalendarService("CNBlogsMeeting");
            calendarService.setUserCredentials("Email", "Password");

            EventEntry entry = new EventEntry();

            //日曆標題與內容
            entry.Title.Text = content;
            entry.Content.Content = content;

            //開始與結束時間,17:00~18:00
            When eventTime = new When(DateTime.Now,
                DateTime.Now);
            entry.Times.Add(eventTime);

            //需要邀請的参會者
            //Who who = new Who();
            //who.Email = "cho88hk@gmail.com";
            //who.Rel = "http://www.yahoo.com.hk";
            //entry.Participants.Add(who);

            //给被邀請者發送通知
            //entry.Notifications = true;

            //被邀請者可以修改該日曆項
            //entry.ExtensionElements.Add(new GuestsCanModify("true"));

            Uri postUri = new Uri("https://www.google.com/calendar/feeds/default/private/full");
            AtomEntry insertEntry = calendarService.Insert(postUri, entry);

            //Assert.NotNull(insertEntry);
        }

2012年2月9日 星期四

Formatting the GridView Based on the Underlying Data


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // determine the value of the status field
            int status = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "status"));
            // color the background of the row
            switch (status)
            {
                case 0:
                    e.Row.BackColor = Color.LightGreen;
                    break;
                case 1:
                    e.Row.BackColor = Color.Red;
                    break;
                case 2:
                    e.Row.BackColor = Color.Blue ;
                    break;

            }
               
           
        }
}

2012年2月7日 星期二

HtmlAgilityPack 抓取HTML上的資訊 asp.net c#

HtmlAgilityPack 是可以簡單抓取html上特定位置資訊。

先在project>add reference 加上HtmlAgilityPack.dll
再配合firebug 的XPATH


//apple store iphone 4s 為例
                    string str = "http://store.apple.com/hk/browse/home/shop_iphone/family/iphone/iphone4s";


 HttpWebRequest httpWebRequest = WebRequest.Create(str) as HttpWebRequest;
                try
                {
                    
                    HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

                    Stream stream = httpWebResponse.GetResponseStream();

                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    string s = reader.ReadToEnd();
                    reader.Close();
                    stream.Close();
                    httpWebResponse.Close();

                    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

                    htmlDoc.LoadHtml(s);
                    //16GB 32GB 64GB 的運送時間XPATH
                    HtmlNode anchors = htmlDoc.DocumentNode.SelectSingleNode(
"/html/body/div[2]/div[3]/div/div[2]/div[2]/div[3]/ul/li/label/span/span[3]/span");
                    HtmlNode anchors32 = htmlDoc.DocumentNode.SelectSingleNode(
"/html/body/div[2]/div[3]/div/div[2]/div[2]/div[3]/ul/li[2]/label/span/span[3]/span");
                    HtmlNode anchors64 = htmlDoc.DocumentNode.SelectSingleNode(
"/html/body/div[2]/div[3]/div/div[2]/div[2]/div[3]/ul/li[3]/label/span/span[3]/span");

//output

txtStatus.Text += anchors.InnerText + " " + anchors32.InnerText + " " + anchors64.InnerText + " ";

                }
                catch (WebException web)
                {
                    
                    //error message
                }