以下代码用于使用自定义验证器验证DropDownList控件。
Default1.aspx
Default1.aspx
<td>
<asp:DropDownList ID="DDL_Product" runat="server" Height="21px" Width="128px">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Nokia</asp:ListItem>
<asp:ListItem>LG</asp:ListItem>
<asp:ListItem>Samsung</asp:ListItem>
<asp:ListItem>sony</asp:ListItem>
<asp:ListItem>Micromax</asp:ListItem>
<asp:ListItem>Karbonn</asp:ListItem>
<asp:ListItem>Apple</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:CustomValidator ID="cv1" Display="Dynamic" ControlToValidate = "DDL_Product" OnServerValidate="ddl_server" runat="server" ForeColor="Red" ErrorMessage="Please Select the Product"></asp:CustomValidator>
</td>
Default1.aspx.cs
protected void ddl_server(object sender, ServerValidateEventArgs e)
{
if (e.Value.selectedIndex <= 0)
{
e.IsValid = true;
}
else
{
e.IsValid = false;
}
}
上述验证未进行验证。 我不知道如何使用此控件并验证 DropDownList。请纠正错误。
DropDownList
是否有选定的值? - EmmanuelRC