如何添加一个RequiredFieldValidator到DropDownList控件? [英] How to add a RequiredFieldValidator to DropDownList control?
问题描述
我把的DropDownList
我绑定使用的SqlDataSource
控制从数据库中显示的值。
I took the DropDownList
and I binded the control using SqlDataSource
to display the values from the database.
当我使用的RequiredFieldValidator
验证我无法使它工作。
When I am validating using RequiredFieldValidator
I am unable to make it work.
推荐答案
有关你对待它,如果你是验证任何其他类型的控制,但使用必填字段校验器的与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" />
基本上就是它说的是,如果在DropDownList选择比与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屋!