Kind of new to Web Development with ASP.Net and was wondering...
-
Why on earth does every form I create run the Form_Load event when I click a button on the form? Is there something I am missing in the switch from Windows Forms to Web Forms? I feel like a right n00b on this but it is driving me mad to find that everytime it clears the form before trying to run the code. A bit of further explanation... I have a web-form application that dynamically sets itself up depending on who is looking at it. If an item in a list is selected and then a button clicked that should take you to another form, instead we are taken back through the form setup, which of course reloads the data back into the lists and therefore removes the selectedItem references. So if anyone can help me stop my app doing this inane activity I will be grateful.
-
Why on earth does every form I create run the Form_Load event when I click a button on the form? Is there something I am missing in the switch from Windows Forms to Web Forms? I feel like a right n00b on this but it is driving me mad to find that everytime it clears the form before trying to run the code. A bit of further explanation... I have a web-form application that dynamically sets itself up depending on who is looking at it. If an item in a list is selected and then a button clicked that should take you to another form, instead we are taken back through the form setup, which of course reloads the data back into the lists and therefore removes the selectedItem references. So if anyone can help me stop my app doing this inane activity I will be grateful.
You need to understand that the way Windows executes program is completely different from the way you will see in your Web(ASP.Net) application. The first thing one should write on the wall before starting any web application is that Web is stateless[^](or Google for a better answer). Here [^]is a very good explanation regarding the order of events in ASP.Net page life.
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
Why on earth does every form I create run the Form_Load event when I click a button on the form? Is there something I am missing in the switch from Windows Forms to Web Forms? I feel like a right n00b on this but it is driving me mad to find that everytime it clears the form before trying to run the code. A bit of further explanation... I have a web-form application that dynamically sets itself up depending on who is looking at it. If an item in a list is selected and then a button clicked that should take you to another form, instead we are taken back through the form setup, which of course reloads the data back into the lists and therefore removes the selectedItem references. So if anyone can help me stop my app doing this inane activity I will be grateful.
By the sounds of it you need to check the isPostBack property of the Page object. This is used to tell if its the first time the page is loaded or its a postback. If its the first time you would want to do things like your dynamic setup of the form i.e. fill in items in a dropdown box or create dynamic controls but you wouldnt want to do this everytime the page loads, otherwise you end up added the same information twice to the page. Look at the Asp.net Page Life Cycle from that you will find that the page load event will run when before the onclick event of the button. See here http://msdn.microsoft.com/en-us/library/ms178472.aspx[^] If you want the button to redirect to another page then you can use Response.redirect("Somepage.aspx") or Server.Transfer("Somepage.asx") Each has its own merits i would depend on what you are trying to achive. Hope that helps Phil