Sharepoint中DateTimeControl控制項DateChanged事件實現(沒有辦法的辦法)

來源:互聯網
上載者:User

前一段時間自訂一個Form頁面,用到DateTimeControl控制項,開始看了MSDN其DateChange事件可以這麼使用:

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);
        }

後來一跑,還真的可以!第一次發貼,希望對大家有所協助。

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.