Scenario:
Monthly filter webpart to filter any list which contains datetime column.
Solution:
This solution depends upon one of the column of the list you want to filter. You need to create a new column name 'Month' with the following calculated formula.
=MONTH(Created)&"/"&"1"&"/"&YEAR(Created)Code:
using System;Scope for improvements:
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Web.UI;
namespace MonthlyFilter
{
public class MonthlyFilter : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
string fieldName = "Month";
NameValueCollection variables = new NameValueCollection(this.Page.Request.QueryString);
string queryParameter1 = "FilterField1";
if (!string.IsNullOrEmpty(variables[queryParameter1]))
variables.Remove(queryParameter1);
string queryParameter2 = "FilterValue1";
if (!string.IsNullOrEmpty(variables[queryParameter2]))
variables.Remove(queryParameter2);
variables.Add(queryParameter1, fieldName);
variables.Add(queryParameter2, "{0}");
string queryString = string.Empty;
foreach (String s in variables)
{
queryString += s + "=" + variables[s] + "&";
}
output.WriteLine(@"<div style='background-color:yellow;float:right'>");
int year = DateTime.Today.Year;
for( int month=1 ; month <=12 ; month ++ )
{
DateTime tempdate = new DateTime(year, month, 1);
output.WriteLine(string.Format("<a href='{0}?{1}'>{2}</a> | ", this.Page.Request.Path, string.Format(queryString, tempdate.ToString("M/d/yyyy")), tempdate.ToString("MMM")));
}
output.WriteLine(@"</div>");
}
}
}
A lot infact, but to start with making the field name, format, styling more generic can be helpful. I am tired ..will do it later.
0 comments:
Post a Comment