如何添加一个RequiredFieldValidator到DropDownList控件? [英] How to add a RequiredFieldValidator to DropDownList control?
问题描述
我有一个 DropDownList
与 SqlDataSource
绑定以显示数据库中的值。
I have a DropDownList
binded with aSqlDataSource
to display the values from the database.
我无法使用 RequiredFieldValidator
进行验证。
推荐答案
在大多数情况下,您将其视为验证任何其他类型的控件,但使用必需字段验证器的InitialValue属性。
For the most part you treat it as if you are validating any other kind of control but use the InitialValue property of the required field validator.
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />
基本上说的是,如果选择了在InitialValue中的1集合中的任何其他值,验证将成功在下拉列表中。
Basically what it's saying is that validation will succeed if any other value than the 1 set in InitialValue is selected in the dropdownlist.
如果数据绑定,您将需要插入请选择值如下
If databinding you will need to insert the "Please select" value afterwards as follows
this.ddl1.Items.Insert(0, "Please select");
这篇关于如何添加一个RequiredFieldValidator到DropDownList控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!