前一段时间自定义一个Form页面,用到DateTimeControl控件,开始看了MSDN其DateChange事件可以这么使用:

Public event DateChanged
Occurs when the date selected in the control changes.

自己傻傻的按照MSDN介绍就在后台代码中给加上

页面控件:

<SharePoint:DateTimeControl ID="dtStartDate" runat="server" AutoPostBack="true">
        </SharePoint:DateTimeControl>

后台代码:

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.dtStartDate.DateChanged += new EventHandler(dtStartDate_DateChanged);

        }

        protected void dtStartDate_DateChanged(object sender, EventArgs e)
        {
            //DateChanged之后处理代码
        }

不知道是MSDN骗俺还是俺功力不够,死活都不起作用。后来花了两三个小时找资料尝试各种各样的办法,

实在不行,只能用最后一招了:反编译Microsoft.SharePoint.dll看看DateTimeControl的结构,终于有了重大的发现

 protected override void CreateChildControls()
    {
        this.Controls.Clear();
        this.m_dateTextBox = new TextBox();
        this.m_dateTextBox.ID = this.ID + "Date";
        this.m_dateTextBox.MaxLength = 0x2d;
        this.m_dateTextBox.CssClass = this.CssClassTextBox;
        if (this.TabIndex > 0)
        {
            this.m_dateTextBox.TabIndex = this.TabIndex;
        }
        this.m_hoursDropDown = new DropDownList();
        this.m_hoursDropDown.ID = this.ID + "DateHours";
        if (this.TabIndex > 0)
        {
            this.m_hoursDropDown.TabIndex = this.TabIndex;
        }
        this.m_minutesDropDown = new DropDownList();
        this.m_minutesDropDown.ID = this.ID + "DateMinutes";
        if (this.TabIndex > 0)
        {
            this.m_minutesDropDown.TabIndex = this.TabIndex;
        }
        this.Controls.Add(this.m_dateTextBox);
        this.Controls.Add(this.m_hoursDropDown);
        this.Controls.Add(this.m_minutesDropDown);
        this.m_validatorRequired = new RequiredFieldValidator();
        this.m_validatorRequired.Display = ValidatorDisplay.Dynamic;
        this.m_validatorRequired.ErrorMessage = this.m_errorMessage_validatorRequired;
        this.m_validatorRequired.ControlToValidate = this.m_dateTextBox.ID;
        this.m_validatorRequired.Visible = this.IsRequiredField && !this.m_timeOnly;
        this.m_validatorRequired.EnableClientScript = this.IsRequiredField && !this.m_timeOnly;
        this.Controls.Add(this.m_validatorRequired);
        this.m_dateTextBox.TextChanged += new EventHandler(this.ChangesByUser);
        this.m_hoursDropDown.SelectedIndexChanged += new EventHandler(this.ChangesByUser);
        this.m_minutesDropDown.SelectedIndexChanged += new EventHandler(this.ChangesByUser);
    }
原来DateTimeControl中由一个TextBox和两个DropDownList组成的,只能采取以下代码实现了

        protected TextBox txtStartDate;
        protected DropDownList ddlStartDateHours;
        protected DropDownList ddlStartDateMinutes;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            txtStartDate = (TextBox)dtStartDate.FindControl("dtStartDateDate");        
            ddlStartDateHours = (DropDownList)dtStartDate.FindControl(dtStartDate.ID + "DateHours");
            ddlStartDateMinutes = (DropDownList)dtStartDate.FindControl(dtStartDate.ID + "DateMinutes");
            this.txtStartDate.TextChanged += new EventHandler(dtStartDate_DateChanged);
            this.ddlStartDateHours.AutoPostBack = true;
            this.ddlStartDateHours.SelectedIndexChanged += new EventHandler(dtStartDate_DateChanged);
            this.ddlStartDateMinutes.AutoPostBack = true;
            this.ddlStartDateMinutes.SelectedIndexChanged += new EventHandler(dtStartDate_DateChanged);
        }

后来一跑,还真的可以!第一次发贴,希望对大家有所帮助。

 

 

 

 

 

posted on 2009-10-29 23:57  南海之滨  阅读(816)  评论(0编辑  收藏  举报