ValidationSummary not working with Login control - asp.net

I'm following this walkthrough from Microsoft:
"Walkthrough: Creating a Web Site with Membership and User Login"
http://msdn.microsoft.com/en-us/library/879kf95c(v=vs.100).aspx
My Login control works and authenticates me successfully. It also shows the little validation *'s if I type the password wrong, so it seems like it is validating. However, the ValidationSummary that is associated with this Login control never displays any of the logon errors.
My code is like this. I set the ValidationGroup just like it said in the Walkthrough:
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Login1" />
<asp:Login ID="Login1" runat="server"></asp:Login>
Can anyone let me know what I'm missing?
Thanks!
Sandra

The ValidationGroup for the child controls will be set to the UniqueID of the Login control. Since you're using a master page, this will not be the same as the ID. Instead, it will be something like ctl00$Login1.
The simplest workaround is to set the ValidationGroup from the code-behind:
ValidationSummary1.ValidationGroup = Login1.UniqueID;
Alternatively, you can customize the LayoutTemplate and manually specify the ValidationGroup on the child validator controls.

Related

ASP ChangePassword control changepasswordfailuretext does not show

I'm using the ASP ChangePassword control. When a user fails to enter their current password correctly I just get a control refresh with all fields cleared, not the changepasswordfailuretext. Googled this and found that you should use ChangePasswordError method to catch this. I put a breakpoint in this method and did not get stop here when the current password was incorrect.
How do I show my users that they have entered their current password incorrectly?
Just ran into this myself and found an answer. If you are customizing the display using ChangePasswordTemplate and SuccessTemplate, you also need to add a control with the id "FailureText". This control gets populated with your configured message in the event of an error:
<asp:Literal ID="FailureText" runat="server" />
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.changepassword.changepasswordtemplate.aspx

asp:RequiredFieldValidator makes postback with empty fields

I've faced the following problem with my aps validator. I have a textbox with asp validator. When the user leaves the textbox empty and click on the submit button the validation message shows, but the page does a postback. Any ideas what may cause this happen?
Heres the validator:
<asp:RequiredFieldValidator ID="valReqName" runat="server"
ControlToValidate="txtName" Display="Dynamic" ErrorMessage="blq blq">
</asp:RequiredFieldValidator>
<asp:TextBox ID="txtName" style="font-family:Tahoma, Geneva, sans-serif;
color: #4F4F4F;" runat="server">
</asp:TextBox>
Many thanks,
Anton
EDIT
I have two tabs in an UpdatePanel. The validation problem is part of the second tab, where the user makes some inputs. When I've remove this trigger:
<asp:AsyncPostBackTrigger ControlID="lnkUpload" EventName="Click" />
The postback problem was solved, but another problem occurred. Well at least the postback is removed. Thanks for all answers.
(lnkUpload is the id of the second tab linkbutton)
EDIT 2
Well here is the solution. It appears that my problem was the same like in this article. http://jeffreypaarhuis.com/2011/08/08/validation-not-working-in-updatepanel/
I'm posting the solution as it might be useful for someone else.
"This is the problem:
When a validator is loaded on the page it creates a bit of javascript to support the clientside validation. When you place a validator inside an usercontrol that isnĀ“t visible by default, and this usercontrol is in an updatepanel, it does not create that javascript properly.
This is the solution:
Outside the updatepanel, I did above, create a dummy validator with a dummy textbox using a dummy validationgroup like so:"
<%--dummy validator to make ajax validation possible--%>
<asp:RequiredFieldValidator runat="server" CssClass="hidden" ControlToValidate="dummyTextBox" ValidationGroup="dummy"></asp:RequiredFieldValidator>
<asp:TextBox runat="server" ID="dummyTextBox" CssClass="hidden"></asp:TextBox>
It appears that my problem was the same like in this article.
Have you tried the solution in this thread:
ASP.net RequiredFieldValidator not preventing postback
Remove the following line from web.config
<xhtmlConformance mode="Legacy"/>
"If I remove the line, my validation works the way I expected it to. Googling that uncovered a bunch of blog posts about how VisualStudio adds that line to the web.config when upgrading web apps from .net 1.1 to .net 3.5.
The blog posts were mainly complaining about how that field interferes with .net's AJAX stuff, but I'm guessing it messes with the JavaScript emitted for the RequiredFieldValidator in a similar fashion."
Please add ValidationGroup to your validator(valReqName) and button.
Add this attribute to your validator.
EnableClientScript="True"

Server side validation

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>
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.
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.

asp.net sanitizing user input

Does asp.net have a built in mechanism that can sanitize all textbox input instead of redirecting to the error page?
I have a textbox input where a user can enter a name, but if they try to enter and <> tags the page automatically throws an error. I just want to handle this error in a user friendly way.
You'll want to look at the AntiXSS library for that. It's a dll so it's easy to drop in and start using it.
The download is at CodePlex.
You can use the ASP.NET RegularExpressionValidator control with a pattern like: ^[^<>]*$
<asp:RegularExpressionValidator ID="rev" runat="server"
ControlToValidate="txtBox"
ErrorMessage="The <> tags are not allowed!"
ValidationExpression="[^<>]*" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
The RequiredFieldValidator is used in conjunction with the RegularExpressionValidator to prevent blank entries. If that textbox is optional, and only needs to be validated when something is entered, then you don't have to use the RequiredFieldValidator.
The benefit of doing it this way is that the error can be handled gracefully and the user can be notified on the same page.
However, if you need to do this for many textboxes and you just want to present something nicer than the error page, you could handle the ValidateRequest error to provide a friendlier message and keep the user on the same page (not just replace it with a custom error page). For more info, check out Kirk Evans' post: Handling ValidateRequest errors within a Page (refer to the section titled Overriding the OnError Method).
Read this for a step-by-step: http://yourtahir.wordpress.com/2008/03/28/aspnet-not-allow-html-in-text-boxserver-error-in-application-a-potentialy-dangerous-requestform-value-was-detected/
You have to do some web.config work.
ASP.net has validation controls
[http://msdn.microsoft.com/en-us/library/7kh55542.aspx][1]
Also there is Mark Down Editor which is a control that strips out html tags etc.

Can't change an asp page because of a required validator

On one of my asp.net pages I have a few textboxes and a required validator attached to them. Everything is ok with validation, but when I want to change page to another required validator doesn't allow it because I don't fill in a textbox :/
How to allowed change page without fill a validate textbox? I must create a condition in "exit page" (how is it named ?) event disabled required validation?
Please help ;)
If you're using some control to move to the next page, set it's CausesValidation property to False.
For example, if you're clicking a button that moves you to the next page, it would be like:
<asp:LinkButton id="myButton" runat="server" CausesValidation="False" .... />

Resources