RequiredFieldValidator not working for Dropdownlist

心已入冬 提交于 2019-12-10 18:12:23

问题


I have a Dropdownlist in my web page as follows.

<asp:DropDownList runat="server" ID="cboNoOfSubjects" AutoPostBack="true" CssClass="textEntry"  width="80%" onclick="javascript:shouldsubmit=false;" ValidationGroup="valCourseFees">
   <asp:ListItem Value="0">-Select-</asp:ListItem>
   <asp:ListItem Value="1">1</asp:ListItem>
   <asp:ListItem Value="2">2</asp:ListItem>
   <asp:ListItem Value="3">3</asp:ListItem>
   <asp:ListItem Value="4">4</asp:ListItem>
   <asp:ListItem Value="5">5</asp:ListItem>
</asp:DropDownList>
    <font color ="red">*</font> 

<asp:RequiredFieldValidator ID="cboNoOfSubjects_RequiredFieldValidator" 
 runat="server" ErrorMessage="No. of Subjects Required" ForeColor="Red"
 Font-Size="0.9em" ControlToValidate="cboNoOfSubjects" 
ValidationGroup="valCourseFees" Display="None"></asp:RequiredFieldValidator>

But the validator does not seem to fire. Can anyone help me with this?


回答1:


For all RequiredFieldValidators, the InitalValue property is important. Add it to yours and it will work.

<asp:RequiredFieldValidator ID="cboNoOfSubjects_RequiredFieldValidator" runat="server"  
 ErrorMessage="No. of Subjects Required" ForeColor="Red" InitialValue="0"
 Font-Size="0.9em" ControlToValidate="cboNoOfSubjects" 
 ValidationGroup="valCourseFees" Display="None"></asp:RequiredFieldValidator>

One thing more to change, you have to set the property Display="Dynamic" which will display accordingly.




回答2:


You are using Display="None" which means the control is not displayed. You have to Use ValidationSummary control to show the error message.



来源:https://stackoverflow.com/questions/22165174/requiredfieldvalidator-not-working-for-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!