Reputation: 783
I am having an issue with the requiredfieldvalidator control not working on an ASP.net page. I have completed the attributes of that field properly, but when I test it, the postback is allowed to happen even if the field in question is blank.
So I want to do server side validation instead. What is the best way to do that? In the event that caused the postback? Also, if I find out the field is blank, how do I get the user back to the screen with all other values they placed on other fields intact and a message saying "This field cannot be blank".
EDIT:
This is the code:
<asp:TextBox ID="fName" TabIndex="1" runat="server" Width="221px" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="FNameRequiredFieldValidator" runat="server" ControlToValidate="fName" InitialValue="" ErrorMessage="Filter Name cannot be blank." ToolTip="Filter Name cannot be blank.">*</asp:RequiredFieldValidator>
Upvotes: 1
Views: 101
Reputation: 10588
To enable Client-side Validation, set the EnableClientScript="true"
on the RequiredFieldValidator
.
You should also always validate on the server side too. But the RequiredFieldValidator
doesn't let you do any special-handling server-side. Just check if Page.IsValid()
. This will return false
if the field is not supplied.
If you want to do custom validation, use a CustomValidator
.
Upvotes: 0
Reputation: 5664
You need to provide the markup for your Button / Link control as well.
The 'CausesValidation' attribute is not supposed to be used on TextBox controls.
The button you click needs to have that attribute set to "True".
Please provide that markup and then I can advise on the alternate server side validation.
Upvotes: 1