-2

Need your help. I am trying to validate drop down list using RequiredField Validator. If user has not selected any value then it should show error. However i am not able to achieve this task.

ASPX code:

<asp:dropdownlist id="ddlSPOC" runat="server" 
     skinid="SkDrpLst" width="210px" height="22px" 
     AppendDataBoundItems="True" validationgroup="Validation">
</asp:dropdownlist>

<asp:requiredfieldvalidator id="rfvddlSPOC" runat="server" 
     controltovalidate="ddlSPOC" InitialValue="0" 
     errormessage="*Please Select SPOC" enabled="false" 
     ForeColor="Red"></asp:requiredfieldvalidator>

Any Help would be appreciated.

Note: I have 7-8 controls on same page and RFV working properly for them.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Helpful tip: use the preview to see what your question will look like, before you post it. Also use the tools for formatting, and read the general help. That way, your code won't be missing because you failed to format it correctly. – J. Steen Mar 08 '17 at 13:45
  • Possible duplicate of [How to add a RequiredFieldValidator to DropDownList control?](http://stackoverflow.com/questions/2280559/how-to-add-a-requiredfieldvalidator-to-dropdownlist-control) – jomsk1e Mar 08 '17 at 13:54
  • Well _Enabled=False_ seems a bit incorrect. (if you want to validate anything) https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.enabled(v=vs.110).aspx – Steve Mar 08 '17 at 13:55
  • @Steve as this dropdown won't be available to everyone. I am making "Enable=True" on page_load events according to logged in user's role. – Vishal_chorghe Mar 08 '17 at 15:03
  • @jomsk1e Tried given link. didn't worked for me – Vishal_chorghe Mar 08 '17 at 15:04

1 Answers1

1

I think you should remove initialvalue=0 like below code

<asp:requiredfieldvalidator id="rfvddlSPOC" runat="server" 
 controltovalidate="ddlSPOC" 
 errormessage="*Please Select SPOC" enabled="true" 
 ForeColor="Red"></asp:requiredfieldvalidator>

because we are already assigning the value so it is reading as value. Might be thats why validation is not working. Please try and let me know.

Thanks :)