2012-01-26 23 views
1

我有一个绑定到数据库的DropDownList。我也手动添加一个项目“(other)”asp.net CustomValidator服务器端不会停止我的程序?

当用户选择“(other)”时,JQuery触发并且.Show()隐藏<asp:TextBox>用户必须在其中输入内容。

我正在尝试验证此TextBox。当然,因为我使用的客户端只是隐藏它,我不能使用的RequiredFieldValidator +的RegularExpressionValidator所以我尝试了其中的CustomValidator我不是很熟悉:

protected void validatorOther(object sender, ServerValidateEventArgs e) 
{ 
    if (dropdownVisitorType.SelectedItem.ToString() == "(other)") 
    { 
     e.IsValid = (textboxOtherVisitorType.Text != ""); 
    } 
} 

protected void buttonRegister_Click(object sender, EventArgs e) 
{ 
    //a whole bunch of code here... 
} 

然后从我的aspx

<asp:CustomValidator runat="server" id="validatorOtherVisitorType" ValidateEmptyText="true" onservervalidate="validatorOther" errormessage="*" /> 

当我尝试调试时,看起来e.IsValid将成功返回false。然而,我的网页似乎只是忽略它,并继续进行,使验证器无用。我究竟做错了什么?

回答

3

您需要强制的注册按钮点击确认:

this.Page.Validate(); 
if (this.Page.IsValid) 
{ 
// your registration logic. 
} 
+0

这正是我需要的。非常感谢! – Baxter