日历控件中日期修改
大家好,做了一个竞拍活动,每天只有一个产品进行竞拍,所以每个月中一天对应一个产品,界面如下,和大家分享一下;
界面:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
///<summary>
///MyCalendar 的摘要说明
///</summary>
namespace CustomControls
{
publicclass MyCalendar : System.Web.UI.WebControls.Calendar, System.Web.UI.INamingContainer
{
privateobject m_dataSource;
publicobject DataSource
{
get { return m_dataSource; }
set { m_dataSource = value; }
}
privatestring m_dataMember;
publicstring DataMember
{
get { return m_dataMember; }
set { m_dataMember = value; }
}
privatestring m_dayField;
publicstring DayField
{
get { return m_dayField; }
set { m_dayField = value; }
}
private ITemplate m_itemTemplate;
public ITemplate ItemTemplate
{
get { return m_itemTemplate; }
set { m_itemTemplate = value; }
}
private ITemplate m_noEventsTemplate;
public ITemplate NoEventsTemplate
{
get { return m_noEventsTemplate; }
set { m_noEventsTemplate = value; }
}
private TableItemStyle m_dayWithEventsStyle;
public TableItemStyle DayWithEventsStyle
{
get { return m_dayWithEventsStyle; }
set { m_dayWithEventsStyle = value; }
}
private DataTable m_dtSource;
public DataTable DtSource
{
get { return m_dtSource; }
set { m_dtSource = value; }
}
public MyCalendar()
: base()
{
//
//TODO: 在此处添加构造函数逻辑
//
this.SelectionMode = CalendarSelectionMode.None;
this.ShowGridLines =true;
}
privatevoid SetupCalendarItem(TableCell cell, DataRow r, ITemplate t)
{
CalendarItem dti =new CalendarItem(r);
t.InstantiateIn(dti);
//数据绑定
dti.DataBind();
//添加数据
cell.Controls.Add(dti);
}
protectedoverridevoid OnDayRender(TableCell cell, CalendarDay day)
{
if (m_dtSource !=null)
{ //格式化
DataView dv =new DataView(m_dtSource);
dv.RowFilter =string.Format("{0} >= #{1}# and {0} < #{2}#",
this.DayField, day.Date.ToString("MM/dd/yyyy"),
day.Date.AddDays(1).ToString("MM/dd/yyyy"));
if (dv.Count >0)
{ //添加样式
if (this.DayWithEventsStyle !=null)
{
cell.ApplyStyle(this.DayWithEventsStyle);
}
//加载模板
if (this.ItemTemplate !=null)
{ //加载模板的控件
for (int i =0; i < dv.Count; i++)
{
SetupCalendarItem(cell, dv[i].Row, this.ItemTemplate);
}
}
}
else
{ //加载模板
if (this.NoEventsTemplate !=null)
{
SetupCalendarItem(cell, null, this.NoEventsTemplate);
}
}
}
base.OnDayRender(cell, day);
}
protectedoverridevoid Render(HtmlTextWriter html)
{ //定义数据源
m_dtSource =null;
if (this.DataSource !=null&&this.DayField !=null)
{ //设置数据源
if (this.DataSource is DataTable)
{
m_dtSource = (DataTable)this.DataSource;
}
if (this.DataSource is DataSet)
{ //数据源为DataSet对象
DataSet ds = (DataSet)this.DataSource;
if (this.DataMember ==null||this.DataMember =="")
{
m_dtSource = ds.Tables[0];
}
else { m_dtSource = ds.Tables[this.DataMember]; }
}
if (m_dtSource ==null) { thrownew Exception("数据源错误。"); }
}
base.Render(html);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
///<summary>
///MyCalendar 的摘要说明
///</summary>
namespace CustomControls
{
publicclass MyCalendar : System.Web.UI.WebControls.Calendar, System.Web.UI.INamingContainer
{
privateobject m_dataSource;
publicobject DataSource
{
get { return m_dataSource; }
set { m_dataSource = value; }
}
privatestring m_dataMember;
publicstring DataMember
{
get { return m_dataMember; }
set { m_dataMember = value; }
}
privatestring m_dayField;
publicstring DayField
{
get { return m_dayField; }
set { m_dayField = value; }
}
private ITemplate m_itemTemplate;
public ITemplate ItemTemplate
{
get { return m_itemTemplate; }
set { m_itemTemplate = value; }
}
private ITemplate m_noEventsTemplate;
public ITemplate NoEventsTemplate
{
get { return m_noEventsTemplate; }
set { m_noEventsTemplate = value; }
}
private TableItemStyle m_dayWithEventsStyle;
public TableItemStyle DayWithEventsStyle
{
get { return m_dayWithEventsStyle; }
set { m_dayWithEventsStyle = value; }
}
private DataTable m_dtSource;
public DataTable DtSource
{
get { return m_dtSource; }
set { m_dtSource = value; }
}
public MyCalendar()
: base()
{
//
//TODO: 在此处添加构造函数逻辑
//
this.SelectionMode = CalendarSelectionMode.None;
this.ShowGridLines =true;
}
privatevoid SetupCalendarItem(TableCell cell, DataRow r, ITemplate t)
{
CalendarItem dti =new CalendarItem(r);
t.InstantiateIn(dti);
//数据绑定
dti.DataBind();
//添加数据
cell.Controls.Add(dti);
}
protectedoverridevoid OnDayRender(TableCell cell, CalendarDay day)
{
if (m_dtSource !=null)
{ //格式化
DataView dv =new DataView(m_dtSource);
dv.RowFilter =string.Format("{0} >= #{1}# and {0} < #{2}#",
this.DayField, day.Date.ToString("MM/dd/yyyy"),
day.Date.AddDays(1).ToString("MM/dd/yyyy"));
if (dv.Count >0)
{ //添加样式
if (this.DayWithEventsStyle !=null)
{
cell.ApplyStyle(this.DayWithEventsStyle);
}
//加载模板
if (this.ItemTemplate !=null)
{ //加载模板的控件
for (int i =0; i < dv.Count; i++)
{
SetupCalendarItem(cell, dv[i].Row, this.ItemTemplate);
}
}
}
else
{ //加载模板
if (this.NoEventsTemplate !=null)
{
SetupCalendarItem(cell, null, this.NoEventsTemplate);
}
}
}
base.OnDayRender(cell, day);
}
protectedoverridevoid Render(HtmlTextWriter html)
{ //定义数据源
m_dtSource =null;
if (this.DataSource !=null&&this.DayField !=null)
{ //设置数据源
if (this.DataSource is DataTable)
{
m_dtSource = (DataTable)this.DataSource;
}
if (this.DataSource is DataSet)
{ //数据源为DataSet对象
DataSet ds = (DataSet)this.DataSource;
if (this.DataMember ==null||this.DataMember =="")
{
m_dtSource = ds.Tables[0];
}
else { m_dtSource = ds.Tables[this.DataMember]; }
}
if (m_dtSource ==null) { thrownew Exception("数据源错误。"); }
}
base.Render(html);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
///<summary>
///CalendarItem 的摘要说明
///</summary>
namespace CustomControls
{
publicclass CalendarItem : Control, INamingContainer
{
///<summary>
/// 数据行
///</summary>
private DataRow m_dataItem;
///<summary>
/// 构造函数逻辑
///</summary>
public CalendarItem(DataRow dr)
{
m_dataItem = dr;
}
///<summary>
/// 属性:DataItem
///</summary>
private DataRow dataItem;
public DataRow DataItem
{
get { return dataItem; }
set { dataItem = value; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
///<summary>
///CalendarItem 的摘要说明
///</summary>
namespace CustomControls
{
publicclass CalendarItem : Control, INamingContainer
{
///<summary>
/// 数据行
///</summary>
private DataRow m_dataItem;
///<summary>
/// 构造函数逻辑
///</summary>
public CalendarItem(DataRow dr)
{
m_dataItem = dr;
}
///<summary>
/// 属性:DataItem
///</summary>
private DataRow dataItem;
public DataRow DataItem
{
get { return dataItem; }
set { dataItem = value; }
}
}
}