2009-04-16 95 views

回答

10

使用與InitialValue財產,你不希望被選擇的默認項:

<asp:DropDownList ID="ddlItems" runat="server"> 
    <asp:ListItem Text="-Day-" Value="-Day-"></asp:ListItem> 
    <asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem> 
    <asp:ListItem Text="Item 2" Value="Item 2"></asp:ListItem> 
</asp:DropDownList> 

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" 
    ErrorMessage="Validation Message !" ControlToValidate="ddlItems" 
    InitialValue="-Day-"></asp:RequiredFieldValidator> 
+1

冷靜,從來不知道的與InitialValue屬性。謝謝。 – 2009-04-16 12:46:52

1

你可以使用像這樣的CompareValidator:

<asp:DropDownList ID="ddlItems" runat="server"> 
    <asp:ListItem Text="-Day-" Value="-1"></asp:ListItem> 
    <asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem> 
    <asp:ListItem Text="Item 2" Value="Item 2"></asp:ListItem> 
</asp:DropDownList> 

<asp:CompareValidator runat="server" ID="CompareFieldValidator1" 
    ErrorMessage="Validation Message !" ControlToValidate="ddlItems" 
    ValueToCompare="-1" Operator="NotEqual"></asp:CompareValidator> 

-Edoode