using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Sdtcn.WebControls
{
[
Designer(typeof(Design.CompositeControlDesigner))
]
public abstract class CompositeControl :
WebControl, INamingContainer
{
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
}
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace Sdtcn.WebControls
{
/**//**//**//// <summary>
/// CustomCalendar 的摘要说明。
/// </summary>
public class CustomCalendar : CompositeControl
{
private TextBox _calendarTextBox;
private HtmlInputButton _selectInputButton;
private Calendar _calendar;
private static readonly object EventSelectChanged = new object();
private string display
{
get
{
if(ViewState["Display"] == null)
return "none";
else
return ViewState["Display"].ToString();
}
set
{
ViewState["Display"] = value;
}
}
代理子控件的属性代理子控件的属性#region 代理子控件的属性
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("")
]
public string CalendarText
{
get
{
EnsureChildControls();
return _calendarTextBox.Text;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("Select"),
Description("选择日期按钮的名称")
]
public string SelectButtonText
{
get
{
EnsureChildControls();
return _selectInputButton.Value;
}
set
{
EnsureChildControls();
_selectInputButton.Value = value;
}
}
#endregion
事件事件#region 事件
[
Category("Action"),
Description("选择日期后触发的事件")
]
public event EventHandler SelectChanged
{
add
{
Events.AddHandler(EventSelectChanged,value);
}
remove
{
Events.RemoveHandler(EventSelectChanged,value);
}
}
protected virtual void OnSelectChanged(object sender, EventArgs e)
{
EventHandler selectChangedHandler = (EventHandler)Events[EventSelectChanged];
if(selectChangedHandler != null)
{
selectChangedHandler(this,e);
}
}
#endregion
private void _calendar_SelectionChanged(object sender, EventArgs e)
{
_calendarTextBox.Text = _calendar.SelectedDate.ToShortDateString();
System.Web.UI.Control div = Page.FindControl("divCalendar");
if(div is HtmlGenericControl)
{
((HtmlGenericControl)div).Style.Add("display","none");
}
OnSelectChanged(sender,e);
display = "none";
}
private void _calendar_VisibleMonthChanged(Object sender, MonthChangedEventArgs e)
{
display = "block";
}
public CustomCalendar()
{}
protected override void CreateChildControls()
{
Controls.Clear();
_calendarTextBox = new TextBox();
_calendarTextBox.ID = "calendarTextBox";
_calendarTextBox.ReadOnly = true;
_selectInputButton = new HtmlInputButton("button");
_selectInputButton.Attributes.Add("Onclick","OnClick()");
_calendar = new Calendar();
_calendar.SelectionChanged += new EventHandler(_calendar_SelectionChanged);
_calendar.VisibleMonthChanged += new MonthChangedEventHandler(_calendar_VisibleMonthChanged);
this.Controls.Add(_calendarTextBox);
this.Controls.Add(_selectInputButton);
this.Controls.Add(_calendar);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
string script = "<script language=JavaScript> function OnClick(){ if( divCalendar.style.display == /"none/") divCalendar.style.display = /"/"; else divCalendar.style.display = /"none/";}</script>";
Page.RegisterClientScriptBlock("script",script);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
AddAttributesToRender(writer);
_calendarTextBox.RenderControl(writer);
_selectInputButton.RenderControl(writer);
writer.Write("<br>");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute(HtmlTextWriterAttribute.Id,"divCalendar");
writer.AddAttribute(HtmlTextWriterAttribute.Style,"DISPLAY:" + display + ";POSITION:absolute");
_calendar.RenderControl(writer);
writer.RenderEndTag();
}
}
}