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
Related
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
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.
I posted for some help over here:
Avoiding try/catch hell in my web pages
I have added an 'override OnError' in a common base class from all my pages, and can confirm that it works properly by putting a breakpoint within the function. However, I still get an exception propagating to the client, no matter what I do. Here is my issue:
1) I have a Telerik Grid control that is updated based on the value of a ComboBox. If an exception occurs and I catch it in a try/catch block then everything is fine.
2) If I get an exception when the ComboBox is changed but there is NO use of a try/catch block, then the error is caught in OnError. However, now I get a PageRequestManagerServerErrorException error just popping up in the client Ajax response (Javacript).
Any help?
There could be many reasons why it's happening. For one, it could be something with client-side rendering that's causing it, or it could be an AJAX request from an update panel or the RadAjaxPanel... one way to possibly tell is to tap into the Sys.WebForms.PageRequestManager's endRequest event, which is discussed briefly here: http://encosia.com/2007/07/18/how-to-improve-aspnet-ajax-error-handling/
So the error is possibly not happening around that area of code that has the try/catch block, but could be caused from something internal in the Telerik processes...
Could you share any code/markup?
Thanks.
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.
I have a asp.net page with some user controls in it. One of the controls is a search form that has its results populated into a gridview.
The gridview needs to have some extra columns that contain 3 LinkButtons that perform a url calculation and redirect to another page.
The reason that I chose LinkButton over HyperLink is that I wouldn't need to perform all url calculations before the user actually needs, since I have way too many search results and the calculation is a bit expensive.
So, my LinkButtons look nice except when I am using IE6. In this case I get a Javascript error: "Expected ')'" or "Expected ':'".
Has anyone seen this?
I've finally realized that my problem was some jQuery code that interfered with the links, thus producing an error.