如何将RequiredFieldValidator添加到DropDownList控件?

发布于 2024-08-21 16:17:53 字数 136 浏览 2 评论 0原文

我有一个与 SqlDataSource 绑定的 DropDownList 来显示数据库中的值。

我无法使用 RequiredFieldValidator 进行验证。

I have a DropDownList binded with aSqlDataSource to display the values from the database.

I am unable to validate using a RequiredFieldValidator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丿*梦醉红颜 2024-08-28 16:17:53

大多数情况下,您将其视为正在验证任何其他类型的控件,但使用所需字段验证器的 InitialValue 属性。

<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />

基本上,它的意思是,如果在下拉列表中选择了 InitialValue 中设置的 1 之外的任何其他值,则验证将成功。

如果数据绑定,您将需要在后面插入“请选择”值,如下所示

this.ddl1.Items.Insert(0, "Please select");

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" />

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");
孤者何惧 2024-08-28 16:17:53

假设您的下拉列表是:

<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>

有两种方法:

<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0" />

第二种方法是使用比较验证器:

<asp:CompareValidator ID="re1" runat="Server" ValueToCompare="0" ControlToCompare="ddl" Operator="Equal" />

Suppose your drop down list is:

<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>

There are two ways:

<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0" />

the 2nd way is to use a compare validator:

<asp:CompareValidator ID="re1" runat="Server" ValueToCompare="0" ControlToCompare="ddl" Operator="Equal" />
戴着白色围巾的女孩 2024-08-28 16:17:53

如果您使用数据源,则可以使用另一种无需后台代码的方法。

请注意以下关键点:

  • Value="0"ListItem 是在源码页面,不是代码中添加的
  • 源码中的 ListItem如果不包含的话将会被覆盖
    DropDownList 中的 AppendDataBoundItems="true"
  • InitialValue="0" 告诉验证器这是要执行的值
    应该触发该验证器(如其他答案中指出的)

示例:

<asp:DropDownList ID="ddlType" runat="server" DataSourceID="sdsType"
                  DataValueField="ID" DataTextField="Name" AppendDataBoundItems="true">
    <asp:ListItem Value="0" Text="--Please Select--" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvType" runat="server" ControlToValidate="ddlType" 
                            InitialValue="0" ErrorMessage="Type required"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="sdsType" runat="server" 
                   ConnectionString='<%$ ConnectionStrings:TESTConnectionString %>'
                   SelectCommand="SELECT ID, Name FROM Type"></asp:SqlDataSource>

If you are using a data source, here's another way to do it without code behind.

Note the following key points:

  • The ListItem of Value="0" is on the source page, not added in code
  • The ListItem in the source will be overwritten if you don't include
    AppendDataBoundItems="true" in the DropDownList
  • InitialValue="0" tells the validator that this is the value that
    should fire that validator (as pointed out in other answers)

Example:

<asp:DropDownList ID="ddlType" runat="server" DataSourceID="sdsType"
                  DataValueField="ID" DataTextField="Name" AppendDataBoundItems="true">
    <asp:ListItem Value="0" Text="--Please Select--" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvType" runat="server" ControlToValidate="ddlType" 
                            InitialValue="0" ErrorMessage="Type required"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="sdsType" runat="server" 
                   ConnectionString='<%$ ConnectionStrings:TESTConnectionString %>'
                   SelectCommand="SELECT ID, Name FROM Type"></asp:SqlDataSource>
著墨染雨君画夕 2024-08-28 16:17:53

InitialValue="0" :当在 ddl 中选择第 0 个索引项时,将触发初始验证。

<asp:RequiredFieldValidator InitialValue="0" Display="Dynamic" CssClass="error" runat="server" ID="your_id" ValidationGroup="validationgroup" ControlToValidate="your_dropdownlist_id" />

InitialValue="0" : initial validation will fire when 0th index item is selected in ddl.

<asp:RequiredFieldValidator InitialValue="0" Display="Dynamic" CssClass="error" runat="server" ID="your_id" ValidationGroup="validationgroup" ControlToValidate="your_dropdownlist_id" />
且行且努力 2024-08-28 16:17:53

如果您想检查空字符串,请使用以下命令:

<asp:RequiredFieldValidator ID="REQUIREDFIELDVALIDATOR1" ControlToValidate="ControlId" runat="server" ValidationGroup="groupName" InitialValue="" Display="Dynamic" ErrorMessage="Please select" SetFocusOnError="true"></asp:RequiredFieldValidator>

If you are looking to check with empty string then use the following:

<asp:RequiredFieldValidator ID="REQUIREDFIELDVALIDATOR1" ControlToValidate="ControlId" runat="server" ValidationGroup="groupName" InitialValue="" Display="Dynamic" ErrorMessage="Please select" SetFocusOnError="true"></asp:RequiredFieldValidator>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文