Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Postbacks in ASP.NET

Tags:

asp.net

For example,

If I have a textbox with runat=server on a page, The value will be posted back to the server so I can access the properties in the code-behind.

However, under the following situations, does it still hold true?

  • A textbox with runat=server but does not appear in the function that is post back to. For example, a button is also on the page, when clicked a post back occurs and within the method that is raised, this textbox was not used.
  • Within a MasterPage, will a textbox residing on the Masterpage itself be posted back?

Because just thinking, isn't this mechanism bloated in nature?

If all input controls and its value are posted back on every single button click (even when the input control is not needed), doesn't this deteriorate performance?

Having just one Form Tag on the page really restricts us to using this mechanism?

like image 873
TimLeung Avatar asked Sep 12 '26 10:09

TimLeung


2 Answers

Truly Understanding ViewState is a must read article on the subject of ASP.NET ViewState

like image 166
Russ Cam Avatar answered Sep 15 '26 04:09

Russ Cam


There are several options to cut down on the bloat (and yes, there's a lot of it when dealing with lots of controls):

  1. Use AJAX to post only the items required - although be careful to allow clients that don't have JavaScript enabled to still use the page/ site.
  2. The MVC framework allows multiple form tags to be used so you can group sections if needs be.
  3. Set the EnableViewState to false on pages/ controls.
  4. Break up your pages into smaller ones.

Additionally, check out this brilliant graphical representation of the Page Life Cycle in ASP.NET.

like image 27
Kieron Avatar answered Sep 15 '26 04:09

Kieron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!