Saturday, June 25, 2011

Compute the total and display it in GridView footer.

There are various way to calculate total in tht GridView Footer. But this method is for simple and easiest way to calculate.

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Collections.Generic"%>
<script runat="server">

private decimal amountSum;

protectedvoid Page_Load(object sender, EventArgs e)
{
List<account> data =new List<account>();
data.Add(new Account("Tom",12002));
data.Add(new Account("Sam", 8900));
data.Add(new Account("Harry", 15558));

gridView.DataSource = data;
gridView.DataBind();
}

string incrementSum(decimal amount)
{
amountSum += amount;
return"";
}

public class Account
{

public Account(string name,decimal amount)
{
this.name = name;
this.amount = amount;
}

privatestring name;
private decimal amount;

public decimal Amount
{
get { return amount; }
set { amount = value; }
}

publicstring Name
{
get { return name; }
set { name = value; }
}

}

</script>
<html>
<head id="Head1" runat="server">
<title>GridView Summary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gridView"
AutoGenerateColumns="false" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<%# Eval("Amount","{0:c0}") %>
<%# incrementSum((decimal)Eval("Amount")) %>
</ItemTemplate>
<FooterTemplate>
<%# amountSum.ToString("c0") %>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Wednesday, June 22, 2011

Export a DataTable to Excel in ASP.NET

All spreadsheet applications (Excel, Calc etc.) understand semicolon separated files natively, so everyone can use this method – you don’t even have to use Excel for it to work.

public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();

foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);

foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}

context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
context.Response.End();
}

Then just call this method and pass the DataTable and the filename as parameters.

ExportToSpreadsheet(table, "products");

The method is static so you can use it anywhere in a web application. Put it on a page, a HTTP Handler, add it to the App_Code folder or stick it in a separate assembly. As long as you call it from a web application it will work.

Tuesday, June 21, 2011

Get current page name from URL

This function can be used to retrieve the current page name from the URL:
name, i.e default.aspx, hello.aspx or whatever.

public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}

By going through System.Web.HttpContext.Current object we are able to have this function in a generic dll or class - and not in each and every page needing to call it.


Tuesday, June 7, 2011

Microsoft Internet Information Services versions 4.0 to 6.0

Open Internet Service Manager or Internet Information Services (IIS) Manager.
If necessary, expand the Web server that you want, and then expand Web Sites.
Right-click the Web site that you want to change.
Click Properties.
Click the Web Site tab.
Change the TCP Port Number in the TCP Port edit box (or click Advanced for multiple Port settings).
Click OK to save the changes.

Microsoft Internet Information Services 7.0

Open Internet Information Services (IIS) Manager.
Select the Web site that you wish to configure.
In the Action pane, click Bindings.
Click Add to add a new site binding, or click Edit to change an existing binding.

Click OK to apply the changes.