Custom Validator not firing - c#

I realize there are lots of similar posts, however I have not found one that has worked for me unfortunately. Basically, I have an asp:customvalidator that I am trying to add to a validationgroup with other validators so that all error messages appear in the same alert. Here is the customvalidator
<asp:TextBox runat="server" ID="txtVideo1Url" Columns="20" Width="98%" />
<asp:CustomValidator runat="server" ID="valURL1" ControlToValidate="txtVideo1Url" OnServerValidate="txtVideo1Url_ServerValidate" Display="None" ValidationGroup="submission" />
and here is the event
protected void txtVideo1Url_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = false;
valURL1.Text = "FAIL!";
}
The event isn't firing at all and I have no idea why. Once I can get the event firing I can put some actual logic into it, lol
UPDATE: I've noticed that I am now able to get the event firing, however the validationsummary is set to display all errors in a messagebox and this error isn't getting added to the messagebox.

Remember to set this property on the CustomValidator...
ValidateEmptyText="True"

You need to set the CausesValidation property of the TextBox to true, like this:
<asp:TextBox runat="server" ID="txtVideo1Url" Columns="20" Width="98%" CausesValidation="true" />

You will have to add ValidationGroup="submission" to the ASP.NET control that will fire the postback.

CustomValidators don't fire if other Validators in your ASPx are not validating. You may need to force a Page.Validate("something"), with your specific validation group. I suggest look at OnTextChanged event to force a page validate.

Related

Bind control's Enable property to a CheckBox's Checked property

I want to set the Enable property of a RequiredFieldValidator control, depending on the Checked property of a CheckBox control. My controls are wrapped in an UpdatePanel. If I write the following code, everything works fine.
ASPX/HTML:
<asp:CheckBox
ID="chkIsEmailSubscribed"
runat="server"
OnCheckedChanged="chkIsEmailSubscribed_CheckedChanged"
AutoPostBack="true" />
<asp:TextBox
ID="txtSubscriptionEmail"
runat="server" />
<asp:RequiredFieldValidator
ID="rqrSubscriptionEmail"
runat="server"
ControlToValidate="txtSubscriptionEmail"
ErrorMessage="Email is required" />
Code-behind:
protected void chkIsEmailSubscribed_CheckedChanged(object sender, EventArgs e)
{
rqrSubscriptionEmail.Enabled = chkIsEmailSubscribed.Checked;
}
But I want to achieve this without writing any code in the code-behind and instead doing it in the HTML. I want to replace my code-behind logic with the following binding expression:
<asp:RequiredFieldValidator
...
Enabled="<%#chkIsEmailSubscribed.Checked%>" />
But this binding expression doesn't work as I expected. What's wrong with this?
I suggest you try to display the value of <%#chkIsEmailSubscribed.Checked%> from your front-end output.

requiredfieldvalidator stops valid event

I have a requiredfieldvalidator for a dropdownlist. Code:
<asp:DropDownList ID="ddlSoortGebeurtenissen" runat="server"
DataSource="<%# SoortGebeurtenissen %>" CssClass="inputtext"
CausesValidation="False" ValidationGroup="valGroupSelectControls"
DataTextField="Title" DataValueField="ID" AutoPostBack="True"
OnSelectedIndexChanged="ddlSoortGebeurtenissen_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredValidatorSoortGebeurtenis" runat="server"
ErrorMessage="Invullen van Soortgebeurtenis is verplicht"
ToolTip="Invullen van Soortgebeurtenis is verplicht"
ControlToValidate="ddlSoortGebeurtenissen"
Display="Dynamic"
CssClass="ui-icon-errormsg"
Text="Invullen van Soortgebeurtenis is verplicht"
ValidationGroup="valGroupSelectControls" />
The selectedIndexChanged event fires when needed, except when the validation has been done before. When I press the submit button having selected an empty value, the validator nicely shows me the error message. Then when I change the selection, and expect the selectedIndexChanged event to fire, it doesn't fire. Nothing happens until I trigger another postback. (lets say the close button). Then the selectedIndexChanged event is fired AND the event of the actual button is fired.
Somehow the selectedIndexChanged event sticks around until the next postback, but does not cause the postback itself. This only happens when the validation shows the error beforehand.
Remove CausesValidation="False" property from dropdownlist
Try using
EnableClientScript="False"

FreeTextBox javascript error is preventing PostBack on my DropDownList

I have one page with 3 FreeTextBox controls on it. They are set up correctly, and I was using them normally until I needed to add a DropDownList control that would PostBack to the server, but I was surprised to see that the OnSelectedIndexChanged event would never trigger. If I was to do a Post with a button, or some other server-side control, then would the event be triggered. After much debugging I found the following Javascript error was being thrown every time I selected something different on my DropDownList control:
TypeError: FTB_API.MainContent_MainContent_FreeTextBox1 is undefined
The error seems pretty straight forward;
Firebug tells me this error comes from the following function:
function WebForm_OnSubmit()
{
FTB_API['MainContent_MainContent_FreeTextBox1'].StoreHtml();FTB_API['MainContent_MainContent_FreeTextBox2'].StoreHtml();FTB_API['MainContent_MainContent_FreeTextBox3'].StoreHtml();
return true;
}
I've tried several things without success. When I remove the FreeTextBox controls from my page, I have successful PostBacks. Any help would be appreciated.
Thanks.
EDIT 1: This is some of my markup
3 FreeTextBox set up like this:
<FTB:FreeTextBox ID="FreeTextBox3" JavaScriptLocation="ExternalFile" ButtonImagesLocation="ExternalFile" ToolbarImagesLocation="ExternalFile" runat="server" EnableHtmlMode="true" />
My DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
Set the property AutoPostBack = true of your dropdown in markup page. This will make the post back when you change the dropdown element and OnSelectedIndexChanged get triggered.
Example :
<asp:DropDownList id="drpList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="event name" />
I have found the answer to the question in this thread: Hidden FreeTextBox bug on Firefox It seems for some reason that when the control is not visible or is hidden (I have tabs) it behaves this way. The answer is kind of a Hack, but it works. Thanks for the answers.

How to bypass validation for a button in ASP.NET?

I have an ASP.NET form that takes input from a user. There's a Submit button on the form and a button called Calc which does a calculation to populate a text field. The problem I'm having is that on the form I have a set of <ASP:REQUIREDFIELDVALIDATOR> validators and when the Calc button is pressed the form gets validated. I don't want the required fields to be validated when the Calc button is pressed, only the Submit button. Any way around this?
Set the CausesValidation property to false.
<asp:Button runat="Server" ... CausesValidation="False" />
Button.CausesValidation (If I remember correctly).
Try putting CausesValidation="false" as a button attribute.
Some sample code:
http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx
ASPX Page:
<asp:Button ID="buttonNew" runat="server" Text="New" CausesValidation="False" />
OR
CodeBehind Page: (.cs)
buttonNew.CausesValidation = false;
Check here to know more about Validated and Validating events for the controls.
Set the button.causesValidation to false.
this link
However, if all it is doing is calculating something based on user input then you shouldn't have it posting back at all. I would recommend using an HTML button and attach some javascript to it to do your work for you and then you won't have this problem.
While designing the button, you can set its property CausesValidation="false" to avoid validation on button click event. It does not allow to validation the server control and perform its click event only
You should use this
UseSubmitBehavior="False"
To disable validation in a specific control
Set the control's CausesValidation property to false.
<asp:Button id="Button3" runat="server"
Text="Cancel" CausesValidation="False">
</asp:Button>
To disable a validation control
Set the validation controls Enabled property to false.
To disable client-side validation
Set the validation controls EnableClientScript property to false.
For More Info

C# web application event handling

Which type of event would i place on a textbox to cause an action on the web form when the cursor leaves that textbox ?and how can i implement this?
I actually want to display a message on the form after details have been entered in the last textbox to notify users if they have left any field blank. I hope to apply this on the last textbox on the form.
I know an event handler shoud be able to instantiate this but am not sure which event would do this and how to implement it....
all advices are warmly welcome..
Thank you..
I think you are probably going to want to look at the ASP.NET validation controls. They should be able to handle what you are wanting to do.
http://devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=46
http://msdn.microsoft.com/en-us/library/aa479045.aspx
You'll need to use javascript on the client to handle this. What you want to do is add a handler for the blur event. The blur event occurs when an element loses focus. Use this in conjunction with your client-side validation logic to trigger validation when the field loses focus.
I prefer adding my javascript unobtrusively. Below is an example of how you would do it using jQuery and the jQuery validation plugin. Using it with standard ASP.NET validators would work as well, just replace the call to the validation logic with that for your client-side validators, i.e., call Page_ClientValidate().
<script type="text/javscript">
$('form').validate(); // set up validation
$('#lastTextBoxID').blur( function() {
$('form').valid(); // validate when the blur event happens
});
</script>
You can put the textbox inside a div and add ur function in the event of onmouseover()
like this :
<div onmouseover="ChangeFocus()">
<asp:TextBox runat="server" ID="TxtBox1"></asp:TextBox>
</div>
<script type="text/javascript" language="javascript">
function ChangeFocus()
{
var Details = document.getElementById('<%=TxtBox1.ClientID %').text;
//Display the details of the textbox in the place u need
}
</script>
And take care to adjust the width of the div and the textbox to fit it.
or u can replace asp textbox with input field of type "text" and set it's event onmouseover to the function ChangeFocus().
Hope that this will be usefull
You could do something like this:
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="The name field is required."
ControlToValidate="txtName" Display="None">
</asp:RequiredFieldValidator>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
Could the TextChanged event be a likely solution? and when would this event occur?
I tried that but it didnt work... It appears that the event fires after the page has been validated (on button click).
any work arounds?

Categories