ASP.NET 1.X to ASP.NET 2.0: Broken Validation on Postback - asp.net

Good Day,
We have migrated our web application to ASP.NET 2.0 from ASP.NET 1.1.
We have a page that contains a couple of textboxes with their respective Validators.
On .NET 1.1, when a textbox contains an INVALID value, clicking on the "submit" button, will not produce a postback (E.G. Nothing will happen).
However, when we migrated to .NET 2.0, even if there is an INVALID value, the postback will still happen. (E.G. Pressing the "submit" button will perform a postback).
Is there an issue with validation when migrating from 1.1 to 2.0?
Additional Infos:
The "submit" button is an input button:
<input type="button">
Using an <asp:button> in place of the <input> button will work and will fix the problem. However, the <input type="button"> has the capability to call a javascript that will produce a "Wait... Loading" label overlay on the page. Using the asp:button, the "Wait... Loading" overlay javascript will NOT be invoked.
EDIT: Real problem and solution.
Anyway, the real problem is that the on-click validation javascript was broken during migration.
The original and working script is:
<input language="javascript" onclick="{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('m_bt_Save','')} " name="m_bt_Save" id="m_bt_Save" type="button" value="Save" width="80px" height="24px" />
But ASP.NET 2.0 changed it to:
<input onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(''); __doPostBack('m_bt_Save','')" name="m_bt_Save" type="button" id="m_bt_Save" value="Save" width="80px" height="24px" />
So the my solution was, change the INPUT button to an ASP:button, then add the attribute doing page load with the correct ASP.NET 1.1 javascript validation like so:
m_bt_Save.Attributes.Add("OnClick", "if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('m_bt_Save','')");

One thing you can do, is you can just add your JS that you need to the button.
Button1.Attributes.Add("OnClick", "do my js stuff here");
That will allow you to have the exact same functionality as the input, and it makes it work.
Otherwise, my guess is that you will need to modify the way the input button submits to call the validate page method before it does the from submit.

You can use page validators from the javascript.
See this link:
http://www.aspdotnetfaq.com/Faq/How-to-control-ASP-NET-Validator-Controls-Client-Side-validation-from-JavaScript.aspx
http://www.codedigest.com/Articles/ASPNET/166_Restrict_AspNet_Validator_controls_to_Fire_on_Page_Submit.aspx

This happened in our upgrade as well. I think we narrowed it down to a problem with the WebUIValidation.js file. I think re-installing it was the final fix for us.

Related

LinkButton not firing ASP.NET validators

I have a form that currently uses an control to submit a form. Everything works perfectly. So now the new requirement is for the "submit' button to be a link. Changing it to a LinkButton control, without changing a SINGLE other thing, breaks the validation.
There is a bit too much code to post in a SO question and I know there's a bit of a lack of detail here, but is there any reason why a LinkButton wouldn't fire ASP.NET validation the same way a Button control would? In theory, they should both operate exactly the same, no?
The current submit button:
<asp:Button ID="btnSubmit" TabIndex="9" Text="Send" ValidationGroup="Forward" runat="server" />
The new submit button:
<asp:LinkButton ID="btnSubmit" TabIndex="9" Text="Send" ValidationGroup="Forward" runat="server" />
The Link button should fires the validation the same way a normal button does, my concerns in your case would be the following:
make sure these is nothing in the server side code stopping this.
make sure in the javascript code there is nothing stopping the "
ASP.NET controls that fire validation has a property called CauseValidation
Be sure all controls should fire validation, has this property set to True
Add attribute CauseValidation="True" to your control but if you want to fire this at particular line at code behind you can use validate the form by the following code:
FormID.Validate();
I know this is old but it has never answered. Did your validator have a "controlTovalidate"? Currently it would appear as if the validator was not firing but in reality it is. It just does not have anything that it is 'watching'. Hope if anyone reaches this thread that this helps even if it is just a little bit.
I was unable to determine the cause of this issue but was able to solve it:
I set the CausesValidation="false" and added at the top of the onclick event this.Validate(linkButton.ValidationGroup) this allows the event to get to the code behind and validation to occur.

jQuery validation plugin and .Net WebForm not playing nicely in IE7, works in IE8

Please bear with me. I've got the jQuery validation plugin working beautifully in FF, Opera, Safari and IE8. However, IE7 is giving me problems. Our back-end developer insisted on using .Net WebForm to create the form on the server (at least this is what I think she used, it's .aspx is about all I know - I can post some code if it'll help.) Anyway, in the code her submit button looks like this:
<asp:Button ID="btnSubmit" runat="server" Text="Submit the request" OnClick="btnSubmit_Click" />
and the output (in the HTML) looks like this:
<input type="submit" name="btnSubmit" value="Submit the request" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="btnSubmit" />
However, when I replace her code with a simple <button type="submit" name="Submit" value="submit" class="button">Submit</button> the validation works, the error messages pop up, clouds part, etc. But she says she can't use this. Sigh.
So I guess my question is: how do I get jQuery to play nicely with her approach? I'm using jQuery to add some nice UI touches to the form, such as accordions, the calendar widget, etc. and I wanted this to be the icing on the cake. I'm also very sure that the initiation of the js is well-formed, I get no js errors for extra comma's, etc.
Thanks for whatever help you can provide.
Based on the javascript that is emitted in the button's onclick event ("if (typeof(Page_ClientValidate)....."), it looks like you are mixing ASP.NET validation and the jQuery Validate plugin. Try adding CausesValidation="false" to the attributes of the button, thereby forcing it to do a normal form submit, to which the Validate plugin responds.
Does the jQuery require the class to be set to "submit" in order for the jQuery validation plugin to work?
That seems to be the most obvious difference between the html generated by .Net and the html you wrote. If that is the case add 'CssClass="submit"' to asp:button element in the aspx page. This will be converted to 'class="submit"' in the generated html.
<asp:Button CssClass="submit" ID="btnSubmit" runat="server" Text="Submit the request" OnClick="btnSubmit_Click" />

Asp.net validator issue

I am using a require field validator for a textbox in asp.net application. Problem is that when there is no value in textbox then no button on the web page do not works even page redirection button for back page also not performing function and validator gives err msg which i define
But if I put sum value in it then all btn works correctly.
Can anyone tell me that how can i overcome this problem
Set the CausesValidation attribute to False on the button and link controls that don't require validation (such as your back button).
<asp:button id="btnBack" runat="server" causesvalidation="false"></asp:button>

How do I make a key binding/shortcut for an ASP.NET button to postback via that button?

I'm developing what's intended to be a very efficient UI in ASP.NET.
I want my users to be able to hit ALT-A to postback the form via a particular button. In other words, when they hit "ALT A" the form will post back and the event handler/function associated with a particular ASP.NET button control will run.
How can I do this? I have ASP.NET 2.0/3.5. I also have the Telerik control set at my disposal.
-KF
Use the "accesskey" attribute in HTML.
<input type="submit" accesskey="a" value="Submit">
<asp:Button AccessKey="a" runat="server" ....

ASP.NET form validation - highlight fields with JS/DHTML

I have an issue that's been bugging me this morning. I'm building an ASP.NET webforms app that has many input forms and I'm trying to standardise how I manage validation. I would like to use the built-in validators (RequiredFieldValidator, Regex etc). My html requirements are:
Before validation:
<div class="formLine">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
</div>
After validation (in case of error):
<div class="formLine formError">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
<span class="errorMessage">Please enter some value</span>
</div>
The additional span is fine, this is achieved with the ASP.NET validation controls. My problem is adding the formError class to the containing <div>.
I'm comfortable using jQuery/DHTML to add this class, but just don't know where to hook it in. Is it possible to monitor DOM changes with jQuery - for example fire an event handler whenever a span is added as a child of <div class="formLine">?
Does anyone else have any good suggestions for dealing with this?
Thanks for reading.
EDIT: After clarification
You could write a custom function that is called by the onClick client side event. Because you are using ASP.Net validation controls you should be able to call the function ValidatorValidate(val) where val is the validator. You could then check the contents of the validators SPAN tag for your validation error and update your div tag accordingly.
Note: I'm no expert on client side validation but this should work in principal.
Further info on MSDN.
You need to declare your div as runat="server" with an id tag so you can reference it in server side code then in page load do a test for Page.IsValid which will return false if one of your validators fails to vaildate.
You can then set the class property on your div programatically. Something like:
<div class="formLine" id="divForm" runat="server">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
</div>
Code-Behind (VB)
If Not Page.IsValid Then
divForm.Attributes.Remove("class")
divForm.Attributes.Add("class", "formLine formError")
End If
i'm assuming you're looking for a way to do this on the client side, without postback.
when you use asp.net validation controls on your webform you'll notice that a script reference to WebUIValidation.js is rendered on your page. peeking into that file offers some useful info. there's a global variable in that file named Page_IsValid. whenever validation fails, that variable is set to false.
EDIT: the WebUIValidation.js script reference was prior to .net 2.0, so you can't look at that file anymore. however, the Page_IsValid variable is still there and can be used by hooking into a form submit handler. something like this
// verified that this works
$(document).ready(function() {
$('#form1').bind('submit', function(e) {
if (!Page_IsValid)
$('#yourDiv').addClass('error');
});
});

Resources