tags:

views:

686

answers:

9

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?

+1  A: 

Every input on the page is posted back fully unless you use ajax, because of the single form tag. Welcome to asp.net...

Shawn Simon
one of my least favourite features (ha) of asp.net, yuck
annakata
+1  A: 

As long as the method that you're hitting on the server-side is a non-static member of the page's class, it'll have access to the textbox and all other controls on the page.

And yes, all controls rendered to the browser (whether in the MasterPage, user control, etc.) will be available on post-back.

You may want to look into Understanding ASP.NET View State.

There surely are performance hits with this architecture, but (depending on complexity of the page) it's usually not an issue from the server load perspective, because hardware upgrades are typically cheaper than additional programming hours spend on optimizing application performance.

With that said, (and as others have pointed out) look into using AJAX if you want to avoid whole page-level postbacks to the server.

Kon
A: 

Yes, it's all posted back, and yes it can cause bloat. I'm sure if you search for ViewState you will find plenty of people ranting about it and how to minimise it :)

Steven Robbins
A: 

You've pretty much hit the nail on the head with vanilla ASP.NET - it's not very good! In both the instances you describe the answer is yes - the textbox will be sent with the form.

The whole postback/ViewState problem is a bit of a pain, one of the first things any competent ASP.NET developer learns to do is avoid them!

roryf
Avoid what, ViewState and postbacks? That's not necessarily true.
Kon
A: 

Yes your text box will be available in both cases, yes it is bloated. This is where AJAX comes into play. Using AJAX you can send just the data you need.

If you want to send a minimal ammount of data, you could use a Page Method (static method on page decorated so the script manager builds javascript to call it or you could call it using jquery or other methods), or a script enabled web service works nice as well.

You also have viewstate which can get very large. ASP.Net MVC is a new paradigm instead of using WebForms which doesn't have view state, or post backs. It embraces HTTP instead of hidding it giving developers more control.

JoshBerke
+2  A: 

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.

Kieron
A: 

The textbox data would be posted back as noted. In addition to using Ajax, disabling view state greatly imporoves your page's performance though even then data in properties critical to the functioning of controls (Control state) would still be posted back.

etsuba
+1  A: 

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

Russ Cam
A: 

If you didn't have postback for every control on the form, you wouldn't be able to access it in code-behind. I.e. if in your button press you wanted to modify the property of the text control you couldn't do that because ASP.Net would know nothing about the text control.

Since the communication between the server and the client is stateless and every time a page is server the server forgets all about it Postbacks are important if you want to work with the same page again. No matter what programming language you use, this or similar mechanism exists for processing server side code.

If you wish to minimize postback (viewstate size), do this.

  • Set enableviewstate=false on all controls that you don't want posted back.
  • Use AJAX and web services wherever possible (and don't use UpdatePanel).
  • Use HTML control as much as possible instead of ASP.Net controls.

Hmm.. There are some excellent suggestion in other answers and good links too.

Cyril Gupta
Ajax without the UpdatePanel? elaborate please
gnomixa
Yeah, MS Ajax is not about updatepanels alone. You can use a simple mix of ASP.Net PageMethods and Webservices + Javascript.
Cyril Gupta