2013-03-23 141 views
0

我在aspx頁面中有一個下拉列表,我使用數據表填充下拉列表。我如何將必填字段驗證器應用於此下拉列表? PLZ幫我驗證下拉列表

+0

請張貼代碼[你已經嘗試過(http://mattgemmell.com/2008/ 12/08 /你有什麼試過/) – 2013-03-23 15:18:29

回答

1

您必須設置InitialValue所選項目的Value是第一選擇:

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ControlToValidate="DropDownList1" 
    InitialValue="-- Please select --" 
    ErrorMessage="Please select something" /> 

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"> 
    <asp:ListItem>-- Please select --</asp:ListItem> 
    </asp:DropDownList> 
+0

我填充下拉使用數據表不manullay ..。 – 2013-03-23 15:27:49

+0

@RinshadHameed:沒關係。你有一個項目,這是你的默認項目類似於我的「請選擇」,不是嗎?請注意,我使用了'AppendDataBoundItems = true',但是您也可以使用['Items.insert(0,「 - 請選擇 - 」)'](http://msdn.microsoft.com/en-us /library/ffx2x2y2.aspx)。但是,使用'InitialValue'屬性來爲'Validator'指定意味着「未選中」的項目。如果您設置了「DataTextField」**和**'DataValueField',那麼也可以是一個ID。例如,默認項目爲「-1」。 – 2013-03-23 15:33:33

+0

謝謝它確實有效.. :) – 2013-03-23 17:12:51