ASP.NET – Working with Gridview TemplateFields
1. code behide
public string ComputeSeniorityLevel(TimeSpan ts)
{
int numberOfDaysOnTheJob = ts.Days;
if (numberOfDaysOnTheJob >= 0 && numberOfDaysOnTheJob <= 1000) return “Newbie”; else if (numberOfDaysOnTheJob > 1000 && numberOfDaysOnTheJob <= 4000) return “Associate”; else if (numberOfDaysOnTheJob >= 4000 && numberOfDaysOnTheJob <= 8000)
return “One of the Regulars”;
else
return “An Ol’ Fogey”;
}
2. html
<asp:GridView ID="GridView1" Runat="server"
DataSourceID="employeeDataSource" AutoGenerateColumns="False"
BorderWidth="1px" BackColor="White" GridLines="Vertical"
CellPadding="4" BorderStyle="None"
BorderColor="#DEDFDE" ForeColor="Black">
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<PagerStyle ForeColor="Black" HorizontalAlign="Right"
BackColor="#F7F7DE"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#6B696B"></HeaderStyle>
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
<Columns>
<asp:BoundField HeaderText="Last" DataField="LastName"
SortExpression="LastName"></asp:BoundField>
<asp:BoundField HeaderText="First" DataField="FirstName"
SortExpression="FirstName"></asp:BoundField>
<asp:BoundField HeaderText="Hire Date" DataField="HireDate"
SortExpression="HireDate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:TemplateField HeaderText="Seniority">
<ItemTemplate>
<%# ComputeSeniorityLevel(DateTime.Now –
(DateTime)Eval("HireDate")) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#CE5D5A"></SelectedRowStyle>
<RowStyle BackColor="#F7F7DE"></RowStyle>
</asp:GridView>
ref: http://msdn.microsoft.com/en-us/library/aa479353(lightweight).aspx
How to use HtmlEncode with TemplateFields, Data Binding, and a GridView
Label ID="LabelDescription"
runat="server"
Text='<%# System.Web.HttpUtility.HtmlEncode((string)Eval("Description")) %>'
Asp.net Permalinks Using URL Rewriting
A Permalink is a permanent link which points to a particular blog entry or forum entry. For example the permalink for this blog article is http://csharp-codesamples.com/2009/03/aspnet-permalinks-using-url-rewriting.
This does not mean that a physical page is created for each blog entry. Instead a single page shows the data for multiple blogs articles dynamically based on some query string.
But all pages in asp.net end with a .aspx extension and if we pass a query string to the page it will look something like Page.aspx?query=test. These links are not very search engine friendly.
This article describes a simple method to create permalinks and improve your asp.net websites search engine optimization using URL rewriting without the use of custom handlers.
ref: http://csharp-codesamples.com/2009/03/aspnet-permalinks-using-url-rewriting/
FileUpload in UpdatePanel, ASP.NET, like Gmail
http://geekswithblogs.net/ranganh/archive/2009/10/01/fileupload-in-updatepanel-asp.net-like-gmail.aspx
ASP.NET Dynamic Data Preview #7: How Do I Use a DynamicControl in ListView and DetailsView Controls?
This video compares the same application written twice, once with Dynamic Data and once without. In the process, you add DynamicControl objects to ListView and DetailsView controls.
http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.dynamiccontrol.aspx
http://www.bestechvideos.com/2008/06/02/asp-net-dynamic-data-how-do-i-use-a-dynamiccontrol-in-listview-and-detailsview-controls
Sorting Generic List
//Sorting List<> case Item is ListItem //============================================== List<ListItem> tempList = new List<ListItem>(); tempList.Insert(0, new ListItem("------------- Vehicle Reports 8")); tempList.Insert(0, new ListItem("------------- Vehicle Reports 2")); tempList.Insert(0, new ListItem("------------- Vehicle Reports 4")); // sort asc tempList.Sort(delegate(ListItem p1, ListItem p2) { return p1.Text.CompareTo(p2.Text); }); // sort desc tempList.Sort(delegate(ListItem p1, ListItem p2) { return p2.Text.CompareTo(p1.Text); }); //Sorting List<> case Item is Object Class //=============================================== //Apply to sorting in GridView //=============================================== SortData(e.SortExpression, NewSortDirection, ref listMsgTicker); //=============================================== #region method for sorting private void SortData(string _expression, SortDirection _direction, ref List<MsgTicker> data) { //sort asc == if (_direction == SortDirection.Ascending) data.Sort(delegate(MsgTicker a, MsgTicker b) { switch (_expression) { case "MsgHeader": return a.MsgHeader.CompareTo(b.MsgHeader); case "CreatedDate": return a.CreatedDate.CompareTo(b.CreatedDate); case "ModifiedDate": return a.ModifiedDate.CompareTo(b.ModifiedDate); default: throw new NotImplementedException("Type of property not implemented yet"); } }); //sort desc == if (_direction == SortDirection.Descending) data.Sort(delegate(MsgTicker a, MsgTicker b) { switch (_expression) { case "MsgHeader": return b.MsgHeader.CompareTo(a.MsgHeader); case "CreatedDate": return b.CreatedDate.CompareTo(a.CreatedDate); case "ModifiedDate": return b.ModifiedDate.CompareTo(a.ModifiedDate); default: throw new NotImplementedException("Type of property not implemented yet"); } }); //return data; } #endregion ref: http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/06/20/How-to-sort-a-generic-List_3C00_T_3E00_.aspx
ThaiBuddhistCalendar and DateTime Convertion
//date time utc DateTime _utc = DateTime.UtcNow; // utc --> thai ThaiBuddhistCalendar tbc = new ThaiBuddhistCalendar(); int thaiDay = tbc.GetDayOfMonth(_utc); int thaiMonth = tbc.GetMonth(_utc); int thaiYearr = tbc.GetYear(_utc); string thaiDaate = thaiDay.ToString("00") + "/" + thaiMonth.ToString("00") + "/" + thaiYearr.ToString("0000"); // thai --> utc DateTime _do, _UtcTime; DateTime.TryParseExact(thaiDaate, "dd/MM/yyyy", new CultureInfo("th-TH"), DateTimeStyles.None, out _do); _UtcTime = _do.ToUniversalTime(); ref: http://msdn.microsoft.com/en-us/library/system.globalization.thaibuddhistcalendar.getera.aspx
Tips to optimize design-time build performance for Web Sites in Visual Studio
There have been a number of posts with tips to improve build performance within Visual Studio 2005. I’ve consolidate these posts and other tips into a single post of techniques for common problems.
ref: http://weblogs.asp.net/bradleyb/archive/2005/12/06/432441.aspx