CustomValidator ServerValidate下拉列表

时间:2012-03-01 23:38:36

标签: asp.net

我试图验证一个下拉列表,如果selectedValue =“0”,那么Isvalid = false但它似乎没有用,是否有人知道这里有什么待办事项

protected void valCountry_ServerValidate(object sender, ServerValidateEventArgs e)
{
    if ((e.Value == "0"))
    {
        e.IsValid = false;
        MasterPage master = Page.Master;
        AjaxControlToolkit.ModalPopupExtender popupExtender = (AjaxControlToolkit.ModalPopupExtender)master.FindControl("ModalPopupExtender1");
        popupExtender.Show();
    }
}

<asp:DropDownList ID="ddlCountries" runat="server">
    <asp:ListItem Value="0" Text="Please Choose" />
    <asp:ListItem Value="New Zealand" Text="New Zealand" />
</asp:DropDownList>
<asp:CustomValidator ID="valCountry" runat="server"
    ControlToValidate="ddlCountries"
    Display="Dynamic"
    ErrorMessage="You must select a Country."
    SetFocusOnError="true"
    ValidationGroup="UserInfo" 
    OnServerValidate="valCountry_ServerValidate">*</asp:CustomValidator>       

1 个答案:

答案 0 :(得分:0)

尝试使用ddlCountry.options[ddlCountry.selectedIndex].value代替e.Value