How to avoid button call event on page refresh
-
When ever i call asp.net button it will do what are the code inside the button. after refresh that page automatically button event call once again run the button event. How can i avoid this problem? Thanks, Nagraj.
-
When ever i call asp.net button it will do what are the code inside the button. after refresh that page automatically button event call once again run the button event. How can i avoid this problem? Thanks, Nagraj.
-
CODE HORROR! if (Page.IsPostBack) { //do stuff } Please don't evaluate a bool to a bool, it's not needed.
ChrisKo wrote:
CODE HORROR!
Why? There is nothing wrong with if(Page.IsPostBack == true ), this documents the intent of the code more explicitly. You could also make the argue that Page is unnecessary.
only two letters away from being an asset
-
ChrisKo wrote:
CODE HORROR!
Why? There is nothing wrong with if(Page.IsPostBack == true ), this documents the intent of the code more explicitly. You could also make the argue that Page is unnecessary.
only two letters away from being an asset
You are doing an unnecessary comparison that can be easily avoided. I haven't actually looked at the IL, but the compiler might be smart enough to ignore that comparison. That is not the main reason for avoiding that type of code though. The main reason is because it's quiet easy to do this by mistake:
if(Page.IsPostBack = true )
And then you've got things happening that you never expected and it's easy to miss this simple istype for hours. I agree that Page could be omitted, but as you mentioned, it makes the code more explicit in intent. It's possible that someone new to ASP.NET development takes over maintenance on this project and doesn't know that the Page object is available to him. I guess I shouldn't have said code horror, but it's something that should be avoided.