0

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

Reahreic
  • 596
  • 2
  • 7
  • 26
  • You could check out http://stackoverflow.com/questions/3107579/displaying-error-message-without-postback and http://stackoverflow.com/questions/777889/on-postback-how-can-i-add-a-error-message-to-validation-summary :) – Chalise Aug 28 '12 at 19:16
  • That first post is over my head. The second one im trying to follow, never knew there was an object that can do that. If this were php id just assign it to a session var and check for it on post back. – Reahreic Aug 28 '12 at 19:30
  • Where in the [ASP.net page lifecycle](http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx) are you checking for the illegal situations? Likewise, when are you setting the error message? – Pete Aug 28 '12 at 20:47
  • I just came across mention of the page lifestyle and how the postback event happens after the load event. Simply moving the function that writes the error message from the session to the asp label into a PreRender event solved the problem as the message was able to be displayed on the posted back page. – Reahreic Aug 28 '12 at 21:28
  • @Reahreic: Page LifeCycle... not lifestyle. Interesting twist though. – NotMe Aug 28 '12 at 21:42
  • My bad the wording lifestyle has been all over my work for the last 3 weeks... it kinda blends in wherever i go now. – Reahreic Aug 28 '12 at 22:01

1 Answers1

0

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

Community
  • 1
  • 1
Chalise
  • 3,656
  • 1
  • 24
  • 37