前一段時間自訂一個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);
}
後來一跑,還真的可以!第一次發貼,希望對大家有所協助。