简体   繁体   English

必填字段验证器不适用于数据绑定下拉列表

[英]Required Field Validator not working for Databound dropdownlist

I have a drop down list with required field validator that is not showing up when the user clicks on the button. 我有一个带有必填字段验证器的下拉列表,当用户单击按钮时该列表没有显示。 The user has to select an item from the drop down list, if he leaves it at --Select One-- the required field validator will fire. 用户必须从下拉列表中选择一项,如果他将其留在--Select One处,则将触发所需的字段验证器。 I noticed that on firebug it shows visibility as hidden. 我注意到在萤火虫上,可见性显示为隐藏。

<span id="MainContent_RequiredFieldValidator1" style="color: red; visibility: hidden;">*</span>

This works for my other dropdownlists that are not Databound. 这适用于我的其他非数据绑定下拉列表。 It shows visibility:visible for this dropdownlists. 它显示可见性:对此下拉列表可见。

 <asp:DropDownList ID="ddlCenter" runat="server" AppendDataBoundItems="True"         
    CssClass="form-control textBoxAsp" DataSourceID="SqlDataSourceCenter" 
    DataTextField="Desc" DataValueField="CenterID">
    <asp:ListItem Text="--Select One--" Value="0" Selected="True" />
 </asp:DropDownList>

 <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1"
     ControlToValidate="ddlCenter"
     Text="*"
     ForeColor="Red"
     ErrorMessage="Center is required."
     InitialValue ="--Select One--"
     ValidationGroup="InsertCenter" />

  <asp:ValidationSummary ID="ValidationSummary1" ForeColor="Red" runat="server" ValidationGroup="InsertCenter" />

   <a runat="server" onserverclick="btnAddCenter_Click" id="btnAddUser" validationgroup="InsertEmployee">Add Center</a>

I tried setting Display="Static" ,but the Validator control's style property is actually set as " visibility:hidden ". 我尝试设置Display="Static" ,但Validator控件的style属性实际上设置为“ visibility:hidden ”。

I tried setting set Display="Dynamic" , but the Validator controls style property is actually set as " display:none " 我尝试设置set Display="Dynamic" ,但是Validator控件的style属性实际上设置为“ display:none

Any ideas? 有任何想法吗?

The Initial Value refers to the Value property of the DropDownList rather than the Text value; Initial Value是指DropDownListValue属性,而不是Text值。 hence try setting the Initial Value of the RequiredFieldValidator to 0. 因此,请尝试将RequiredFieldValidatorInitial Value设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM