Overlay says jQuery is not defined when reloading after validation error - plone

I have a form view with a JavaScript that hides some of its fields on load. If a validation error occurs when I submit it, the form is reloaded with the error message as it should be. After reloadig it, the script is executed and the fields are hidden too.
But when I use the form as an overlay, it fails. I open the overlay, the script is executed and the fields are hidden. I post the form, a validation error occurs, and the form is reloaded with the error message, but this time the script that hides the fields is not executed.
Firebug says four times that jQuery is not defined. But the error is not in my code, it's in the javascript generated by plone. The form has four date fields and they generate the inline code that produce the errors:
<script type="text/javascript">
if (jQuery().dateinput) { ... }
</script>
What can I do to get my script executed after the form is reloaded under those circumstances?
I'm using Plone 4.1.4.
Thanks in advance.

Related

Register JavaScript alert at extreme end using RegisterStartupScript

(Note: Just to clarify the problem is not specifically related to C1WebDateEdit. It could be with any custom control which require JavaScript to render actual control)
I have many C1WebDateEdit controls in my page. On a button click based on some condition I am displaying JavaScript alert message ScriptManager.RegisterStartupScript. These controls are inside UpdatePanel. The issue I am facing with it is, when these C1WebDateEdit has not value and page displays alert message, it displays "01/01/0001" behind the alert box and on closing alert it shows empty textboxes.
The reason is, C1WebDateEdit creates actual control using JavaScript. and page renders alert message JavaScript before C1WebDateEdit controls' JavaScript.
For example:
HTML markup
alert JavaScript
C1WebDateEdit JavaScript
Logical solution is get alert JavaScript after C1WebDateEdit JavaScript because it will allow C1WebDateEdit to load fully before alert box.
I found no inbuilt way in asp.net to change sequence, so I tries solution given here but It didn't work for me.
One possible solution I am thinking that I create Custom or WebUserControl and place at it last at the page in the UpdatePanel and PreRender event I call ScriptManager.RegisterStartupScript to register alert message. Logically I think it will work but trying to find that any one implemented any other solution for it?.
Other possible solution is use "pageLoaded" event of PageRequestManager. I created below function for temporary fix:
function delayedAlertBox(strMsg)
{
var fnc = function (a, b)
{
//remove reference so on next call it do not show previous alerts.
Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(fnc);
alert(strMsg);
}
//add function reference to call on Ajax pageloaded event
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(fnc);
}
and calling in asp.net like simple function as given below:
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "dd", "delayedAlertBox('Hi, how are you?')", True)
But still looking for any other good alternative. If I found other, I will post it.

Clear server validation message before submit

To replicate the issue:
Create an ASP.NET MVC 3 page with server validation.
Submit with validation error.
Server validation error appears in red.
Correct the validation error and submit again, then Success
redirected to another report page.
On clicking the Back button on the report page, the validation error
message still showing on the original form.
Users getting annoyed seeing the server validation message when clicking the back button.
I would not consider a solution to disable the back button.
I would not consider a solution to refresh the page on clicking the
back button.
I would consider the server validation message to disappear before making the successful form submission or any other valid solution.
Thanks
Here are some methods for clearing history so that back button will not show remembered page: http://www.codeproject.com/Tips/135121/Browser-back-button-issue-after-logout
To avoid seeing the validation message, I disable the cache so browser will automatically reload the page when users click Back button. However, users would see a form resubmission warning if the page was previously submitted using POST method. To workaround this, I use javascript window.history.back() to change a POST request back to the prior GET request if the ModelState is not valid.
Here are the details...
Create two functions in controller: one with [HttpGet] attribute and one with [HttpPost] attribute.
In the HttpPost version, add following logics...
If ModelState is invalid, save ModelState and form/model data to TempData
Return a simple View that contains javascript like following.
<html>
<body>
<script>window.history.back();
</script>
</body>
</html>
In the HttpGet version,
Add attribute [OutputCache(NoStore = true, Duration = 0)]
Add logic to load ModelState and model data from TempData
Pass model data to View

Error message being overwritten on postback

When i upload a file to my server i have the codebehind check for several illegal situations, and then i set the text of an asp:label to display the error if one occurs.
The problem is that the file upload posts back even when the if statement fails and an error is written to the label, thus resetting the page to its original state and resetting the message label.
Edit and Update
After accepting that the postback would occur i loaded the error messages into a session variable and on post back checked that variable for an error rand updated the asp:list to show the error message.
The line that updated the asp:label had to be placed into a PreRender event as the Load event happened before the postback had processed. causing the page to have to postback twice to show the message.
U hope this helps anyone. Additionally as mentioned below reading through the ASP Page Life Cycle will do wonders
I would suggest checking out the very comprehensive MSDN article about Validating ASP.NET Server Controls. Basically, you will be adding to your front end the following: asp:ValidationSummary, and asp:CustomValidator with back-end code that handles the "illegal check" and then sets the page IsValid to true or false, depending on the outcome of the customer validate function in the code-behind.
An example on SO here: How to Add Error Message to Validation Summary

AJAX Panel not throwing exceptions

i have just noticed something strange in some asp.net markup.
I have a standard form with a couple of textboxes and a submit button.
When clicked the code behind will attempt to perform some logic and then return.
If the input values are not valid it used to throw an exception.
The moment i wrapped the controls in an AJAX update panel and try to submit bad data, no exception is thrown and the panel returns like nothing was wrong.
Does anyone know how to return this to the previous behavior whilst keeping the update panel?
The expression it is thrown, but its handle by ajax, and now have be come Javascript errors, because now the full page is not loaded, but the error return back from javascript ajax call.
To see them just open your javascript error console :), how ever you do not need the expression as Raj say, you need to find other way to show your errors.
When I won to make some test and find my error, I remove the Ajax Panel for this reason, locate the error, fix them, then place back the Ajax Panel

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.

Resources