I have a form in a MS AJAX ModalPopup Extender, which is in a UpdatePanel (for async loading). Everything works as expected. There is one thing that I am trying to do but not sure how to do it.
On form submission, there is server side validation on top of client side validation. My question is: what is the best way to display validation error messages if any? It would be ideal if the messages could be written to the popup asynchronously so that the popup is still there.
If this is not possible, I am thinking of displaying the messages using the JavaScript alert box.
Any insight and suggestions are greatly appreciated.
John
It's been awhile since I have used the ModalExtender and UpdatePanels but this is possible. Essentially (rust falling out of head), I had a label inside of the modal set to visible=false. Then, on clicking the Ok button, I called a function in the code behind that processed the logic. If the logic failed, then I updated the label text and set visible = true. If the logic passed, I called the close() property of the modal. With both, you have to call updatePanel.update() so that the content is refreshed. Let me see if I can find an example...
Not Exactly what I was looking for, but it may help you get on the right track:
If (logInstance.isNew) Then
result = logInstance.createNewLogEntry()
If (result.ToLower = "success") Then
Response.Redirect("default.aspx?status=1")
Else
saveErrorType.InnerHtml = result
ModalSaveError.Show()
End If
Else
result = logInstance.updatePreviousLogEntry(textReasons.Value)
If (result.ToLower = "success") Then
Response.Redirect("default.aspx?status=2")
Else
saveErrorType.InnerHtml = result
ModalSaveError.Show()
End If
End If
Tommy, thanks for the reply. Instead of replying to you as a comment, I am doing it as an answer, hoping that somebody can tell me if this is the best we can do.
Also it may be of some help to those who face the same situation.
After posting yesterday I spent a couple more hours on it and found a solution, which is basically the same idea as you described in your answer.
The key issue was that I am not using the OK button of the extender. Instead I used a regular linkbutton which triggers a server side click event when clicked. This event closes the popup regardless of the validation result. So my goals was to keep the popup open or at least make it appear to be open and then add whatever validation error message to it.
After I cleared my thought on the issue, I figured I should be able to re-open the popup and populate the form with the data that the user is working on, and adding my validation error messages to it.
That did it!
Thanks again.
Related
I am relatively new in programming and I have a doubt about postback. I searched in some pages, but I couldn't solve my question.
I created a web page and I use postback in it that updates some informations in the page. That works without any problems. However, after the postback is fired, if the user presses F5, a message appears asking to confirm the form re-submit.
Why exactly this message appears and how can I avoid it?
If the user would press F5, I want the page reload, without any alerts.
I'm sorry if my question wasn't clear enough, but I really don't understand postback how much I'd like.. =)
By Default ,
The method is post means it will confirm while reloading the page.
The method is get means it wont ask anything.
Why this happens,Whenever the post method called severe action gonna happen in server,so it just confirms from the user.There is no need in the case of get Method.
I hope u are clear with this solution.
One way to avoid your issue is to place a hidden field on the page. When the form is submitted, check the hidden field. If no value, process the form, and populate the hidden field with a value. If it has a value, then do nothing.
I prefer using an on-click event on a button, instead of using the postback event.
As to why it happens, a refresh sends a request along with the form data back to the server causing another postback.
This is known as page re-submission.
When you refresh the browser, it will resend the last request you did. If it was a POST request (like you do in case of postback) then it will re-post the information but before doing it you'll see the warning message you describe.
To prevent this:
Page.Response.Redirect(Page.Request.Url.ToString(), true);
Which changes the response to a GET instead of a POST. Prevents the 'Form Resubmission' dialog.
I thought in the following code to solve my problem, using a javascript event:
$(document).keydown(function (event) {
if (event.keyCode == 116) {
location.href = 'pedidos.aspx';
return false;
}
});
It solves my problem, because I avoid the postback message as I want and the page is reloaded.
But is this a good solution?
I have a webpage with a textbox and a button and when the button is clicked i get som calculations on the value entered, but i cannot enter another value and see calulations on that new number.
How can I achieve this? Thanks in advance.
From your question it sounds like you want to be able to change the values without having to click the button again, and have the calculations redone. The reason clicking your button works is because the page goes through a Postback The calculations are then processed server side and returned after the page has loaded. What you want to do can be accomplished in a handful of ways including: jQuery or ajax. One thing you may want to look at is jQuery .post This can help you post the values of your form to a processing page and then set the return value on your form. On the other hand since you're using .NET you could also take a look at the Microsoft AJAX Control Toolkit One place you could start is by looking at the Update Panel. Good luck and I hope this helps!
I have some controls inside an asp:UpdatePanel. When a button is pressed to update the repeater data the data may change but it might not though. If the data doesn't change it's hard to tell if you pressed the button or not.
Can someone explain to me how you would add some sort of image or progress bar over the top of the panel to show when processing of the data is happening?
This way at least the user should know that their request has been acknowledged.
The UpgadeProgress control was designed to provide feedback to the user that an async request is occurring. This is the simplest way to accomplish this.
You can also wire into the ScriptManager's pageLoaded event if you want to do something more custom after the response is received. A good tutorial can be found here: http://msdn.microsoft.com/en-us/library/bb398866.aspx
I'm really new to asp.net and have a couple of issues I'm trying to get fixed. I have some programming experience, but it is not asp.net. However, I've been able to follow the code enough to make other changes in the code to fix other issues.
The first is this:
I'm working with a form that has a calculate amount method that gets called when the user inputs a value in an amount text box. The same method gets called when the next control, number of payments, has a value.
So in the two controls:
onTextChanged="ctrlName_textChanged"
Then in the code behind, the textchanged method does:
calculateAmount();
The problem is after the amount is calculated and returns, the focus seems to get reset and the user has to tab all the way back through the form to the place they were.
The textboxes in question are in a panel that starts out hidden and is made visible conditionally.
My apologies if I have not used the proper .net terminology.
It looks like the same issue may be causing my second problem. When the user types in an amount and then tabs and quickly adds the number of payments, you can see the amount get calculated correctly and very shortly displays the proper total in the total amount text box. However, even though it shows for that short time, the tab order again gets reset as well as the total amount value.
I've looked at different methods to try and fix the focus issue.
In the textchanged method, I tried using something like:
Session["myval"] = "someval";
Then tried to check against that in Page_Load with something like:
if(Session["myval"] != null) {
this.NextControl_Name.Focus();
}
but it didn't ever work correctly.
I also tried to set a cookie in that same textchanged method using something like this:
Response.Cookies["myval"].Value = "somevalue";
Then tried to check that in Page_Load using something like the previous if block above but using Request.Cookies["myval"] as the source.
Is there a good reference with some really clear code samples I can look at for this type of implementation?
Thank you in advance,
C.
Sounds like you have a postback problem...
Remember that the web is stateless. This means that when you have a web page rendered out in .NET and you attach an event that executes serverside code... it does an HTTP POST back to the server which is effectively a new page request. The Page_Load method will fire again as well as your bound event. So your onTextChanged event is firing a new request back to the server. This is why you see the focus reset and why when you tab quickly, the value seems to disappear magically.
You can do one of several things, you can implement the UpdatePanel in the AjaxControlToolkit
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/
you can use PageMethods and do your validation with javascript and jQuery (or other js library)
see page method info http://www.geekzilla.co.uk/View30F417D1-8E5B-4C03-99EB-379F167F26B6.htm
Hope this helps
Can I tell, using javascript, whether a user has clicked on the "X" icon on a browser dialog, or the "OK"/"Cancel" buttons? I have code I need to run when the window closes, but it will only run when OK or Cancel are clicked.
I currently capture the onunload event of the window. How can i accomplish this?
window.onunload = function() { alert("unloading"); }
Why do you want to do this? We can probably help you come up with a different design that doesn't require this if you tell us what you're trying to do.
However, to answer your question: it's not possible to catch that event in all cases. You cannot prevent the user from closing the browser or guarantee that your code will execute when they do. You can make it slightly annoying for them, but they can disable javascript, or kill the process, or reboot the computer. Using the unload function is the closest you can come to having some code that runs when the window closes (it will run in cases of normal shutdown or when the user navigates away).
If I understood correctly, your question is about a browser dialog, not the main browser window.
To answer your question, you probably cannot distinguish between the Cancel button and the X button of a browser dialog. They'll both end up just returning a false. If you need this level of control, you should consider writing your own simulated dialog (lightbox) instead of a real JavaScript dialog. Or perhaps look at existing frameworks/plugins with modal dialogs that give you the amount of control you need.
What about if he does ALT + F4?
To the best of my knowledge, you can't detect whether the user closed the dialog by clicking the Cancel button or the [x] button, since neither are exposed to you beyond returning the result of the action (e.g., confirm() as true/false).
You can hook into the document.onbeforeunload event to perform whatever cleanup action you require; I've done so myself by sending an asynchronous XMLHTTP request to the server to make sure the user's session gets cleaned up properly.
It is impossible to catch the closing of the browser and handle every other event that also causes a post back. You can try and many people have before you and failed.
Your best bet is to use onbeforeunload and learn how to deal with session timeouts on your serverside to clean up data.