2011-06-13 60 views
0

我在TextBox,RequiredFieldValidator和Asp.net UpdatePanel中有一個Button。最初,RequiredFieldValidator的Enabled屬性設置爲false,以便在UpdatePanel內單擊按鈕時,驗證不會首次觸發。然而,在UpdatePanel的Button的事件處理程序中,我將TextBox的值設置爲DateTime.Now,同時我正在使用ScriptManager.RegisterStartupScript方法註冊啓動腳本,以通過調用ValidatorEnable函數在客戶端上啓用上述驗證程序。如果TextBox爲空,我還在ClickPanel的UpdatePanel外部有一個Button,RequiredFieldValidator應該觸發它。但是,在單擊UpdatePanel內的Button後,TextBox的值被設置爲最近的日期時間並啓用了驗證程序,但是在從文本框中刪除文本並單擊updatepanel外部的按鈕後,驗證程序未被觸發。Asp.net字段驗證器在部分頁面更新後未觸發

請參考以下代碼。

客戶端代碼:

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <asp:TextBox ID="txtSomeValue" runat="server"></asp:TextBox> 
     <asp:RequiredFieldValidator ID="rqdtxtSomeValue" runat="server" Enabled="false" ErrorMessage="*" 
      ControlToValidate="txtSomeValue"></asp:RequiredFieldValidator> 
     <asp:Button ID="btnGetValue" runat="server" Text="Get Current Date" OnClick="btnGetValue_Click" /> 
    </ContentTemplate> 
</asp:UpdatePanel> 
<asp:Button ID="btnSave" runat="server" Text="Save" /> 

服務器端代碼:

protected void btnGetValue_Click(object sender, EventArgs e) 
{ 
    txtSomeValue.Text = DateTime.Now.ToString(); 

    string script = @"javascript:ValidatorEnable(document.getElementById('" + rqdtxtSomeValue.ClientID + "', true))"; 
    ScriptManager.RegisterStartupScript(btnGetValue, btnSave.GetType(), "btnGetValue_Click", script, true); 
} 

誰能告訴我如何重新啓用部分頁面更新後的現場驗證?

回答

0

您確定驗證程序在btnGetValue返回並更新UpdatePanel後啓用嗎?你如何測試它是否啓用驗證器?您是否嘗試將驗證器設置爲在您的代碼隱藏中啓用?

protected void btnGetValue_Click(object sender, EventArgs e) { 
    txtSomeValue.Text = DateTime.Now.ToString(); 

    rqdtxtSomeValue.Enabled = true; 
    ScriptManager.RegisterStartupScript(btnGetValue, btnSave.GetType(), "btnGetValue_Click", script, true); 
}