更新~ 加入ValidationGroup
Intro
目標是
1. Confirm before PostBack
2. Validator working
Context
這大概沒啥技術性, 就是要找怎麼用
重點在
GetPostBackEventReference: 取得PostBack函式
Page_ClientValidate(string ValidationGroup): 執行頁面驗證, 會回傳成功與否
Page_IsValid: 驗證狀態 (這大概是個Flag可以不用)
根據這些去拼出 JavaScript
最後的 return false 是避免自動PostBack
不知道誰呼叫了 onclick, 如果沒回傳false仍舊會PostBack
public static void ClickSendValidConfirmDisabled(System.Web.UI.Page page, System.Web.UI.WebControls.WebControl ctrl, string confirmText){ string clientScript = ctrl.ClientID + ".disabled='disabled';" + page.ClientScript.GetPostBackEventReference(ctrl, null) + ";"; bool needValid = false; string validationGroup = null; if (ctrl is System.Web.UI.WebControls.IButtonControl){ System.Web.UI.WebControls.IButtonControl ibtnCtrl = ctrl as System.Web.UI.WebControls.IButtonControl; needValid = ibtnCtrl.CausesValidation; validationGroup = ibtnCtrl.ValidationGroup; } clientScript = "if(confirm('" + confirmText + "')){" + clientScript + "}"; if (needValid){ clientScript = "if(Page_ClientValidate('" + validationGroup + "')){" + clientScript + "}"; } ctrl.Attributes["onclick"] = clientScript + "return false;"; }
沒有留言:
張貼留言