我正在用asp.net驗證控件製作一個asp.net頁面。如果驗證失敗,我想顯示一個div(在客戶端)。我在哪裏做這個?我無法找到訪問客戶端上的OnValidate事件的方法。我可以在服務器上執行此操作,但我寧願在客戶端上執行此操作。如何隱藏div(在客戶端)客戶端驗證失敗?
3
A
回答
0
使用ASP.NET custom validator。
如果由於某種原因您不能/不想這樣做,請使用ClientScriptManager.RegisterOnSubmitStatement方法,並確保在客戶端腳本中返回false,如果您驗證的任何內容失敗。
0
,您可以調用從服務器端代碼中使用Page.RegisterClientScriptBlock方法] javascript函數1
像
String scriptString = "<script type='text/javascript'>MakeDivVisible();</script>";
this.RegisterClientScriptBlock("clientScript", scriptString);
然後在javascript函數可以改變的div來顯示像塊
document.getElementById("divId").style.display = 'block';
或者如果你給div屬性runat =「server」,那麼你可以訪問div的s erver side code並將其顯示更改爲塊
divId.Style["display"] = "block";
0
您將需要在客戶端和服務器上進行驗證,因爲您無法保證客戶端已打開JavaScript。以下顯示了在客戶端完成此操作的步驟,因爲在服務器端實現此操作應該是微不足道的。
給出一個簡單的div如以下幾點:
<div id="divErrors" runat="server" style="display: none;">
This should only appear when validation fails.
</div>
添加以下JavaScript到您的網頁:
<script language="javascript" type="text/javascript">
function showErrors() {
if (!Page_IsValid) {
document.getElementById('divErrors').style.display = 'block';
} else {
document.getElementById('divErrors').style.display = 'none';
}
}
</script>
最後,註冊提交腳本調用這個新showErrors功能(在Page_Load事件):
If Not Page.IsPostBack() Then
Dim scriptName As String = "OnSubmitScript"
Dim scriptType As Type = Me.GetType()
Dim script As ClientScriptManager = Page.ClientScript
If Not script.IsOnSubmitStatementRegistered(scriptType, scriptName) Then
script.RegisterOnSubmitStatement(scriptType, _
scriptName, "showErrors();")
End If
End If
相關問題
- 1. PayPal客戶端驗證失敗
- 2. 抓住客戶端驗證失敗
- 3. 當ASP.NET客戶端驗證失敗時,如何隱藏跳動者?
- 4. 在客戶端驗證
- 5. JQuery客戶端失敗localhost
- 6. RESTEasy客戶端:undeploy失敗
- 7. asp.net驗證客戶端或客戶端或服務器端
- 8. 客戶端身份驗證 - 處理客戶端證書
- 9. 如何使客戶端驗證在MVC2
- 10. 客戶端丟失javascript驗證
- 11. 客戶端驗證MVC 5
- 12. jquery客戶端驗證asp.net
- 13. 驗證PayPal客戶端ID
- 14. Reactjs客戶端驗證
- 15. Uploadify的客戶端驗證?
- 16. ASP.NET客戶端API驗證
- 17. 客戶端驗證疑惑
- 18. Telerik asp.net客戶端驗證
- 19. asp.net mvc客戶端驗證
- 20. Recaptcha客戶端驗證
- 21. 驗證的RadioButtonGroup客戶端
- 22. 動態客戶端驗證
- 23. 客戶端驗證不MVC
- 24. ASP.NET MVC3客戶端驗證
- 25. 客戶端驗證的OpenERP
- 26. QuickForm2 - 客戶端驗證
- 27. Silverlight - 客戶端驗證
- 28. Http驗證客戶端庫
- 29. 刪除客戶端驗證
- 30. 客戶端輸入驗證
我只想在驗證失敗時執行此操作。只有提交。並在客戶端。 – cerhart 2010-01-19 13:46:22
然後,您可以在提交函數中將客戶端本身的JavaScript代碼寫入並作爲最後一步返回false。 – rahul 2010-01-19 13:51:01
問題是沒有提交功能。我正在使用asp.net驗證控件。 – cerhart 2010-01-19 14:12:19