Problem with Asp.Net RequireFieldValidator and Javascript WYSIWYG - asp.net

I am using the open source Javascript WYSIWYG from OpenWebWare and Asp.Net RequiredFieldValidator on the TextBox which I am calling the WYSIWYG for. Everything works fine, but the first time I try to submit the form, I get the server-side RFV ErrorMessage "Required", but if I submit a second time, it goes through.
Am I missing something? I would like to have the client-side validation... how can I get the text to register as not empty?

I think the reason for this behavior is that validation code runs earlier than the code that updates underlying TextBox from value of WYSIWYG. So the first time you get the error, then the field is updated and the second time you don't get it. Try removing all the content the second time and I bet you wont get validation error (since the value for validator at the moment is what you actually submitted the first time).
The solution would be to find a JavaScript API call for your WYSIWYG which would force the update of the underlying text box field and call it onclick (client-side) of your submit button or whatever you use for that.

the textarea HTML tag is one of the most unpleasent tags to work with and I'm not 100% sure if the client-side validator will support it, regardless of whether it's a WYSIWYG or not.
I think you'd be best off using a CustomValidator and writing the JavaScript which does the checking manually.
Alternatively you can debug though the JavaScript which is used with FireBug or VS 2008.

Related

Using server side validators with modal dialog (jquery)?

Is there a straightforward way to use server-side validation with ASP.NET's validation controls in a form that's displayed in a modal dialog? I am using jQuery and SimpleModal (in C#, VS2010, .NET 4.0)
I've got a modal form which works fine. I need to use a server-side validation because the logic depends on data specific to the record being accessed
My solution for the project I'm working on now is to use a jQuery ajax call to pass all the form data to the server and get back the validation results before allowing the post to proceed. But this is relatively time consuming to implement, and in some situations I'm dealing with now all the validation code exists already.
The first challenge is that of course the modal dialog will close on a full postback. So you could put an UpdatePanel inside the dialog... without even thinking about this too much, though, I assumed that it wouldn't work out that well. It doesn't. And the form which opens the modal dialog to begin with is already in an UpdatePanel, which further confuses matters.
Anyway, I tried putting the contents of the modal form in an UpdatePanel for the heck of it. It does actually do a partial postback, the dialog remains open, but the contents of the dialog do not get updated with anything I change server side. If I close and re-open the dialog on the same page after testing the validation code, though, its contents are in fact updated to reflect these changes. Obviously the way the dialog is rendered is confusing ASP.NET. Or vice-versa. But this just seems sketchy from the get-go.
Rather than trying to hack my way through this problem I was hoping that others had some suggestions about a better way to approach this. Or just tell me I'm trying to hard too mix apples and oranges and I should keep it all client side (or client side + jQuery ajax) if that is the only sensible thing to do.
The two approaches I've taken:
1) submit the data via ajax, the response includes a success:true or false. If false, there is a message included that details the issue.
2) submit the form normally. If there is a validation problem, but the errors in a hidden div on the page and write JS to check for content in that div when the page loads and display the warning/error as necessary.

How to make ASP.Net custom validators produce an alert

All ASP.Net client validation messages can be shown as an alert by setting the ShowMessageBox="True" property on the ValidationSummary control.
This works fine for anything that happens on the client.
For the custom validators that validate server-side I had assumed that what would happen is that when the page is returned to the browser, ASP.Net would inject some javascript to show the alert box. However this isnt the case.
If you had relied on the message box to show detail and just have a * next to the erroneous field (as per my clients req's) then it wont work as intended.
Does anyone have a solution for doing this? What I want is a way to possibly override the ValidationSummary control to inject javascript onto the page or something like this.
Thanks in advance.
You may use Page.RegisterStartupScript to show alerts after server-side validation.

How do I display an error message in asp.net?

I have a bunch of text boxes and a save button to update something. When I click Save I have code that determines whether they are correctly filled in, in the code behind file.
If they are not correctly filled in, I want to display an error message in the form of an alert.
What is the best way to do this? Pressing the button obviously makes the page postback, so I thought about adding something to the URL like mypage.aspx?errormessage=blahblah but I don't know if this is the best way or even how to do it...
Any suggestions?
Modal alerts are bad, as are postbacks. Try to check as much as possible on the client-side without a round-trip to server. See jQuery Validation plugins for a less intrusive way of validation.
Could you use a CustomValidator to trigger client side script that shows a alert box?
You could use the ClientScript.RegisterStartupScript() method in the server side error handling code to write a javascript snippet which calls alert('message'), something like this
private void ShowErrorMessage(string message)
{
string script = "alert('" + message + "');";
ClientScript.RegisterStartupScript(typeof(MyPage), "errorScript", script, true);
}
But I would recommend you use a validator instead. If you implement your own custom validator, you can make it emit client-side script which can run before the submit, to avoid the postback altogether.
A good thing about validators is that their error messages can be displayed in a ValidatorSummary on the page, avoiding the ugly alert box.
First of all I won't recommend showing an modal alert box to the user.
You can call a javascript function from the server side code and in that function you can pop out the error.
Or you can issue an AJAX request and after the validation on server side you can send back a response to the client.
ASP.NET's various validation controls (with client-side validation enabled), coupled with proper error messages and/or summary message will be good enough for most scenarios.
For 'AJAX feel and behaviour', put the controls into an updatepanel will be easy to implement too.
Use ye olde Validators as much as poss, they render out some javascript to the client so yu can do alot of validation using these controls and only when they are all satisfied does it allow the page to submit.
They do fire on every submit so if you don't want every submit action to fire them you make the controls part of a validation group.
They can validate input using regular expressions, make sure a field has a value and there is a few more as well.
Then there is property to declare a 'message' and a control to show all the validator messages. All very swish and built right into the IDE.
Go check out the validator controls.
Try the following code in your button click event:
string strErr="Error!";//Put your error message here
ClientScript.RegisterStartupScript(GetType(), "scrptName", "javascript: alert('"+strErr+"'); ", true);
It will show an alert message.
Else put a label on your aspx page and set visible false in page_load event.
When error occurs in your button event set the label visibility 'true' and fill the label text with the error message.

Using Yes/No Messagebox in Updatepanel AJAX

I have a requirement where I ask user for confirmation and also display messages.
The programmers used for this were from Windows forms background. Hence have used the MsgBox in every nook and corner. Even in business logic part they have used the Messageboxes which requires Yes/No style confirmation from user.
When we tested the site from the remote machine we found that it gives error of using DefaultDesktopOnly/ServiceNotification. But when tested we found that this is totally different from what we were looking for.
Now my requirement is a confirmation box is shown from the code like Delete record" yes no and based on the reply we take the action.
This is to be done using updatepanel.
As you use this code in several places, I suggest you make a custom control, that takes your message and displays and Update panel with message and yes/no buttons.
Internally set some value for yes, no, cancel... so that you get something just like MessageBox.
Update panel or not, you'll have to attach some javascript that would call confirm() javascript function. Based on it's result you cancel javascript default link/button behaviour...
This will give you something to scratch your head for a start:
http://www.dotnetfunda.com/tutorials/ajax/updatepanel.aspx
That's not really a question, but a requirement.
Anyway ... MessageBox is a Windows function, it is not an HTML or browser function. Now you can mimic it in one of two ways, via a javascript confirm function or via Yes/No buttons and the appropriate event.
Given that your requirements are for something that works in an update panel I'd guess that wiring up javascript events manually for this isn't going to be something you are comfortable with, so I'd suggest an asp:Panel inside the UpdatePanel which has yes and no buttons, with server side events bound to them. In the UpdatePanel logic show this when you want confirmation and hide everything else, the act accordingly.
If you want to do server-side confirms, you'll get into more complicated code generation. First of all, you'll have two views. The first one has a link/button delete but will actually be just a postback to the second view that will display confirmation form with yes/no. In this form, your yes button will actually be your delete action...
But I'd still chose a hybrid (especially if this is a grid we're talking about) of javascript and serverside (since alert() and confirm() are evil from user experience perspective):
you have a linkbutton delete
when user clicks on it, you replace this control with a div, that displays two linkbuttons yes/no
send a postback with one of the two
Addendum
No linkbutton could be just dummy, to hide this confirmation and display delete again - so it means there won't be any server round trip
you could even create a usercontrol that mimics this sophisticated delete link behaviour to make it reusable application wide.

Advanced Search Form Validation in asp.net

I'm looking to make validation for a page in which one or more fields have a value in them. I have an advanced search form in asp.net and I'm trying to pop up an error and not post back if all the fields are empty. I've looked into required fields validators but I'm not sure how to make them work together in a AND type fashion instead of the OR fashion that a validation group of required field validators would imply. I hope this makes sense. Thanks for the help.
i had to do something similar years ago and i was using 1.1 then.
what we ended up doing was creating required field validators but disabling them.
then onload we would loop through the validator dictionary, enable them and check if they passed. if any of them passed we broke off the loop and we continued execution, otherwise, if all of them failed then we displayed a warning. Unfortunately, this would require a postback.
If you want to accomplish this on the client-side then you could write a simple javascript function to take care of it before postback.
for every control, place an onBlur event. javascript will check if there is a value in the field and maintain a flag. then before submission you would check the flag and either allow submission or show warning.
You could just write the javascript validation function yourself to handle this case and attach it to your search button.

Resources