2010/11/24

How to copy codes in my blog

Since I am using the latest syntex highlighter, there is no more flash control for users to copy the code or view code in text mode.

The new way to copy the code is "double click" on the code and it will mark all codes automatically. Then press "Ctrl + C" will do the work.

2010/11/17

SqlDataSource/ObjectDataSource use ViewState

On SqlDataSource's Selecting event:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["UserID"].Value = ViewState["UserID"].ToString();
}

On ObjectDataSource's Selecting event:
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["UserID"] = ViewState["UserID"].ToString();
}

2010/11/12

SyntaxHighlighter broken

My SyntaxHighlighter is broken so those codes looks ugly... I will fix it (hopefully soon enough).

Two DateTime related questions and solutions.

How do I get the month name?
1.
DateTime.Now.ToString("MMMM")
2.
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month)

If you want to use the method 2, don't forget to include the
using System.Globalization;


How to know this Sunday's date? (or Monday, Tuesday... etc)
First, create an extension method.

public static class DateTimeExtensions
{
  public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
  {
    int diff = dt.DayOfWeek - startOfWeek;
    if (diff < 0)
    {
      diff += 7;
    }

    return dt.AddDays(-1 * diff).Date;
  }
}

Then call it like this
DateTime.Now.StartOfWeek(DayOfWeek.Sunday)
so that I can get the date of this week's Sunday.

I have these two questions is because I am creating a Google Calendar-liked calendar . :)


Reference:
Month Name in C#
How can I get the DateTime for the Start of the Week