0
我正在使用C#.net。c#.net驗證兩個文本框不能爲空
我有兩個文本框(textbox1/textbox2)。如果兩者均爲空,則需要顯示錯誤消息。
我試過一個CustomValidator控件,並驗證它在textbox2上。在我的代碼後面我檢查了兩者是否都是空的,如果它返回false(args = false)。但是,當我構建應用程序時,它甚至不訪問按鈕事件。我不能使用這個嗎?
這是我曾嘗試:提前任何幫助
<asp:CustomValidator ID="customValidator" runat="server"
ErrorMessage="You must provide either a phone number or email address."
ControlToValidate="textbox2"
OnServerValidate="PhoneEmailCustomValidator_ServerValidate" />
protected void PhoneEmailCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(texbox1.Text) && string.IsNullOrEmpty(textbox2.Text))
{
Debug.Write("Within if statement");
args.IsValid = false;
}
}
感謝。
克萊爾
你能分享一些代碼嗎? – Zenuka 2009-10-09 08:03:04
添加了我嘗試過的代碼 – ClareBear 2009-10-09 08:27:09