我試圖創建將自定義的驗證傳遞給它的任何TextBox控件的方法。傳遞一個控制的方法
這裏是我到目前爲止有:
自定義驗證
protected void CustomValidatorDelLN_ServerValidate(object sender, ServerValidateEventArgs args)
{
CustomValidator ThisValidator = sender as CustomValidator;
TextBox MyBox = FindControl(ThisValidator.ControlToValidate) as TextBox;
args.IsValid = isValid(MyBox);
}
驗證法
protected bool isValid(System.Web.UI.WebControls.TextBox MyBox)
{
bool is_valid = MyBox.Text != "";
MyBox.BackColor = is_valid ? System.Drawing.Color.White : System.Drawing.Color.LightPink;
return is_valid;
}
代碼編譯OK,但我得到一個
NullReferenceException被使用未處理R代碼裏面
上
bool is_valid = MyBox.Text != "";
當我運行驗證。
我知道我接近(嗯,我想我),但我要去哪裏錯了?
線 '文本框myBox上=的FindControl(ThisValidator.ControlToValidate)的文本框;' - MyBox爲null –
看看FinControl()它不是遞歸的。我不確定你爲什麼在那裏使用FindControl(),你在頁面上嗎?如果是這樣,你可以把runat =「server」放在那個sucka中並按名稱使用它。 – EJC
對不起,我看到你正在使用自定義驗證器,所以你可以驗證任何TextBox。 – EJC