2010/12/18

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.ClearContent();
Page.Response.ClearHeaders();
Page.Response.Clear();
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());
Response.Write(FileContent);
Response.End();

2010/12/17

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.

2010/12/04

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;" />

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

2010/10/22

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">
  <title>xxxx</title>
 </head>
 <frameset cols="20%,80%">
  <frame name="Menu" src=<%= GetMenuFrame()%> />
  <frame name="Content" src=<%= GetContentFrame()%> />
 </frameset>
</html>

2010/07/08

Distinct in DataTable or DataView

How to remove duplicated data? Suppose I have a DataTable (dtMembers) looks like this:
IDNameCityZipCode
01KennyLA12345
02PeterCA54321
03JohnNY13125
04JimmyNY13125







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.

2010/04/22

[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.

2010/04/06

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

2010/03/26

[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();
or
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
}
reader.Close();
reader.Dispose();