Friday, July 8, 2011

Export Data to Excel/CSV file in asp.net

Hi,
We are frquentlu need this requirement to Export the Reports into Excel file.
Here the easy steps to Export to Excel.

1.We may have our Report Data in DataSet or List.

2.Create StreamWriter Object.
StringWriter sw = new StringWriter();
3.Add all Headers of your Report first.
sw.WriteLine("HEADER11,HEADER12,HEADER13,HEADER134,...);
4.Iterate the Data in foreach loop and write as below
For DataSet:

For List:


5.Finally write this to file as follow:

Response.AddHeader("Content-Disposition", "attachment; filename=FileName.csv");
Response.ContentType = "text/csv";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write(sw);
Response.End();

Have a Nice Day...

Thursday, July 7, 2011

Setting Default Button for textbox in asp.net 1.1

Hi,
Today I am posting a small tip for Setting default button for a text box when press Enter in asp.net 1.1.
The method below describes the way to specify a default button to submit when the user hits the enter key in a textbox.
1.Add the below Script.

function clickButton(e, buttonid)
{
var evt = e ? e : window.event;
var bt = document.getElementById(buttonid);

if (bt)
{
if (evt.keyCode == 13)
{
bt.click();
return false;
}
}
}

2.In Code behind Write the following...

TextBox1.Attributes.Add("OnKeyPress", "return clickButton(event,'" + Button1.ClientID + "')");

For Complete details plz refer here