Doing some work after Response.End() (11/14/2013 updated)

I need to do something that after users download a file from my website. Most file downloading approach is using something like this:
//data preparation at here...

Page.Response.Buffer = true;
Page.Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0, 0));
Page.Response.Expires = 0;
Response.AddHeader("content-disposition", "attachment;filename=" + YourFileNameHere + ".xls";
//here I use excel file type
Response.ContentType = "application/vnd.ms-excel";
//FileContent is a string variable which stores your file content
Page.Response.AddHeader("content-length", FileContent.Length.ToString());


Mobile version is on.

Added a mobile version to my blog. If you use your smart phone to browse my blog, it will transfer you to the mobile version of my blog after few seconds.


Tips: Prevent CheckBox from being changed by user

The easiest way to prevent your CheckBox from being changed by users is adding this to your CheckBox's html code:
OnClick="return false;"
For example:
<asp:CheckBox ID="CheckBox1" runat="server" Text="Want a salary increase?" Checked="False"
 OnClick="return false;" />


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.


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();


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?

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
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 . :)

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


Aspx page, Frameset, and QueryString

I encountered a situation that I have to pass values through the query string to an aspx page which contains frameset settings. Here is what I found how to do it (and it works for me).

Let's say I have a Default.aspx which has a frameset settings, and it contains two frames link to Menu.aspx and Content.aspx. Here is the html code for the Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <frameset cols="20%,80%">
  <frame name="Menu" src=<%= GetMenuFrame()%> />
  <frame name="Content" src=<%= GetContentFrame()%> />


Distinct in DataTable or DataView

How to remove duplicated data? Suppose I have a DataTable (dtMembers) looks like this:

Using the following two lines of codes if you want to get the non-duplicated city list:
string[] columnNames = new string[] { "City" };
DataTable dtCity = dtMembers.DefaultView.ToTable(true, columnNames);
The result will be "LA, CA, NY".

Take a look of the DataView.ToTable() method and this discussion.


[SSRS 2005] One table uses two DataSets by using Multivalue Parameter

It's not possible that one report item (table, matrix, list... etc) can use two different datasets. Most people will suggest you to merge them into one dataset, but that doesn't work for me because they are not from the same database.

In my case, one dataset is coming from SQL 2005, and another one is coming from OSIsoft PI Server. I want to create a table that display hourly data for each day by using these two datasets. Not only so, some data are calculated on the fly by refering them. It's like mission impossible to create a report like this: these two datasets don't know each other, and one table cannot be assigned two datasets.

Here is how I solved this problem.


Get .Net Color from Hex Color (Hex string)

It's easy but also easy to forget. I put it here as a note for myself. :)
Label1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFC0FF");


[ASP.NET]The DataSourceMode of SqlDataSource

By default, the DataSourceMode of the SqlDataSource is DataSet. Here is how to retrieve the dataset from the SqlDataSource control:
DataTable dt = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
DataTable dt = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).Table;

If you set the DataSourceMode to DataReader, here is how to read the data:
IDataReader reader = (IDataReader)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
while (reader.Read())
//your code here