Wednesday, September 9, 2009

Date Filter Range Control for SharePoint/ASP.NET


Figure 1



Figure 2
Folks, though I am good in talking with the processor, I am bad at high level languages like Add Image“English”. So please don’t mind my literature and jump into the juice part of it.

I was searching for a Date range filter control with a Start Date and End Date and hit a submit button and filter for those date ranges.
May be I didn’t query well in “google” or “bing”, I didn’t find one. Even that is cool, because, it prompted me to write my own control and here I am.
This is a control, where you can use it in your ASP.NET pages or SharePoint web parts. I wrote it for SharePoint web parts. It has two different Delegates that can be passed in during the initialization time. Based on the delegate, the appropriate control will be shown.
I have two different types of date filter control that needs to be used in my application. One is filter for a week, month, quarterly, half-yearly and yearly.
Please Refer Figure 2
For the above control usage, you need to pass in the method
void SingleDateButtonSubmit (string FilterType)
Here is my Screen Shot of the date range control: Please Refer Figure 1
For the above control usage, you need to pass in the method
void DateRangeButtonSubmit(DateTime StartDate, DateTime EndDate)
Once you pass this delegate, you can handle your functionality appropriately in those methods.
Please feel free to use this control/code and change appropriately per your needs.
If you find it useful, please send your comments/upgrades/changes you made to me.
-------- Code Below----------------------------------------
public class DateFilterRangeControl : System.Web.UI.WebControls.WebControl
{
DropDownList ddlDateFilter;
Button btnSubmit;
Label lblDisplayError;

Microsoft.SharePoint.WebControls.DateTimeControl dtcStart;
Microsoft.SharePoint.WebControls.DateTimeControl dtcEnd;

public delegate void SubmitDelegate(string FilterType);
public event SubmitDelegate evtSubmitDelegate;

public delegate void DateRangeDelegate(DateTime StartDate, DateTime EndTime);
public event DateRangeDelegate evtDateRangeDelegate;


public DateFilterRangeControl(SubmitDelegate evtSD)
{
evtSubmitDelegate = evtSD;
CreateControls();
}

public DateFilterRangeControl(DateRangeDelegate evtSD)
{
evtDateRangeDelegate = evtSD;
CreateDateControls();
}

private void CreateDateControls()
{
this.Controls.Add(new LiteralControl("

"));

using (Table tblMain = new Table())
{
tblMain.CssClass = "DateFilterMainTable";
tblMain.ID = "tblDateFilterMain";
TableRow row = new TableRow();
TableCell cell = new TableCell();
Panel pnl = new Panel();

Label lblfrom = new Label();
lblfrom.Text = "Reporting From:";
pnl.Controls.Add(lblfrom);
cell.Controls.Add(pnl);
cell.CssClass = "tblCellStartDateLabel";
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

pnl = new Panel();
cell = new TableCell();

dtcStart = new Microsoft.SharePoint.WebControls.DateTimeControl();
dtcStart.LocaleId = (int)SPContext.Current.RegionalSettings.LocaleId;

dtcStart.DateOnly = true;
dtcStart.ID = "dtcStartDate";

string noofdays = ConfigurationManager.AppSettings["DaysBackwardFromToday"];
if (noofdays == null noofdays == "")
noofdays = "365";
int intnoofdays = Convert.ToInt32(noofdays);

dtcStart.SelectedDate = DateTime.Now.AddDays(-intnoofdays);
pnl.Controls.Add(dtcStart);
cell.Controls.Add(pnl);
cell.CssClass = "tblCellStartDate";
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

pnl = new Panel();
cell = new TableCell();

Label lblTo = new Label();
lblTo.Text = "To:";
pnl.Controls.Add(lblTo);
cell.Controls.Add(pnl);
cell.CssClass = "tblCellEndDateLabel";
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

pnl = new Panel();
cell = new TableCell();

dtcEnd = new Microsoft.SharePoint.WebControls.DateTimeControl();
dtcEnd.LocaleId = (int)SPContext.Current.RegionalSettings.LocaleId;
dtcEnd.DateOnly = true;
dtcEnd.ID = "dtcEndDate";
dtcEnd.SelectedDate = DateTime.Now;
pnl.Controls.Add(dtcEnd);
cell.Controls.Add(pnl);
cell.CssClass = "tblCellEndDate";
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

pnl = new Panel();
cell = new TableCell();

btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Click += new EventHandler(btnSubmit_Click);
pnl.Controls.Add(btnSubmit);
cell.Controls.Add(pnl);
cell.CssClass = "tblCellSubmit";
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

tblMain.Rows.Add(row);

row = new TableRow();

pnl = new Panel();
cell = new TableCell();

lblDisplayError = new Label();
lblDisplayError.Text = "";
lblDisplayError.ID = "lblDisplayError";
lblDisplayError.Visible = false;
pnl.Controls.Add(lblDisplayError);
cell.Controls.Add(pnl);
cell.ColumnSpan = 3;
cell.CssClass = "tblCellErrorDisplay";

row.Cells.Add(cell);
tblMain.Rows.Add(row);

this.Controls.Add(tblMain);
}

this.Controls.Add (new LiteralControl ("
"));
}

private void CreateControls()
{
this.Controls.Add(new LiteralControl("

"));

Label lblDateFilterText = new Label();
lblDateFilterText.Text = "Select a Date Filter Criteria: ";
this.Controls.Add(lblDateFilterText);

ddlDateFilter = new DropDownList();
ListItem li = new ListItem("Last 7 days", "week");
ddlDateFilter.Items.Add(li);

li = new ListItem("Last 30 days", "month");
ddlDateFilter.Items.Add(li);

li = new ListItem("Last 90 days", "quarter");
ddlDateFilter.Items.Add(li);

li = new ListItem("Last 6 months", "half-year");
ddlDateFilter.Items.Add(li);

li = new ListItem("Last 365 days", "year");
ddlDateFilter.Items.Add(li);

ddlDateFilter.AutoPostBack = true;
ddlDateFilter.CssClass = "Top10DateFilterCombo";


ddlDateFilter.Items.FindByText(ddlDateFilter.Items[1].Text).Selected = true;
ddlDateFilter.SelectedIndexChanged += new EventHandler(ddlDateFilter_SelectedIndexChanged);

this.Controls.Add(ddlDateFilter);

this.Controls.Add(new LiteralControl(" "));

this.Controls.Add(new LiteralControl("

"));
}

void ddlDateFilter_SelectedIndexChanged(object sender, EventArgs e)
{
if (evtSubmitDelegate != null)
evtSubmitDelegate(ddlDateFilter.SelectedValue);
}

void btnSubmit_Click(object sender, EventArgs e)
{
if (evtDateRangeDelegate != null)
{
if (dtcStart.SelectedDate > dtcEnd.SelectedDate)
{
lblDisplayError.Text = "Start Date must be less than or equal to the End Date ";
lblDisplayError.Visible = true;
}
else
{
string strStartDate = dtcStart.SelectedDate.ToShortDateString() + " 12:00:00 AM";
string strEndDate = dtcEnd.SelectedDate.ToShortDateString() + " 11:59:59 PM";
DateTime dtcStartDate = Convert.ToDateTime(strStartDate);
DateTime dtcEndDate = Convert.ToDateTime(strEndDate);
evtDateRangeDelegate(dtcStartDate, dtcEndDate);
}
}
}

private DateTime _dtStartDate;
private DateTime _dtEndDate;
public DateTime StartDate
{
get { return _dtStartDate; }
set { _dtStartDate = value; dtcStart.SelectedDate = value; }
}

public DateTime EndDate
{
get { return _dtEndDate; }
set { _dtEndDate = value; dtcEnd.SelectedDate = value; }
}

}

Usage:
Scenario 1:
DateFilterRangeControl dfrc = new DateFilterRangeControl(ButtonSubmitEvent);
this.Controls.Add(dfrc);
public void SingleDateButtonSubmit (string FilterType)
{
//Handle your functionality
}

Scenario 2:
DateFilterRangeControl dfrc = new DateFilterRangeControl(DateRangeButtonSubmit);
this.Controls.Add(dfrc);

public void DateRangeButtonSubmit(DateTime StartDate, DateTime EndDate)
{
//Handle your functionality
}
-------- Code End ----------------------------------------