0

I'm using the following Compare Validator :

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="DropDownList1"  ErrorMessage="None can't be selected!" 
                    Operator="NotEqual" ValueToCompare="None"></asp:CompareValidator>

over the following Dropdown List:

<asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="181px">
                    <asp:ListItem>None</asp:ListItem>
                    <asp:ListItem>One</asp:ListItem>
                    <asp:ListItem>Two</asp:ListItem>
                    <asp:ListItem>Three</asp:ListItem>
                </asp:DropDownList>

Here's what happens : Initially, nothing happens, and 'None' is selected by default. If I choose any other option and THEN choose 'None', it give me the error.

What I want to do is have the validator display the error right away when the page is first loaded, and not wait for the user to change to some other option and back over to 'None' or wait for the user to submit the form before displaying the form again.

How can I achieve this?

4

2 回答 2

0

尝试这样的事情:

protected void Page_PreRender(object sender, EventArgs e)
{
    Page.Validate();
}

在我的简单测试页面中工作。

于 2012-07-03T09:54:37.190 回答
0

阅读您的方案后,我认为您必须设置 Operator="Equal",如果值为“无”,则会发生错误“无法选择无!”。

于 2012-07-01T16:15:07.087 回答