views:

30

answers:

2

Why is there a need to explicitly call GridView.DataBind() to render the gridview. Why wouldn't it render by itself automatically like how it happens in Window Forms?

+3  A: 

The simple reason is that the ASP.NET sites are stateless.

In Windows Forms your collection can be kept in memory, in ASP.NET that is not the case. The collection has to be rebuilt from post back to post back.

The GridView is different than the web on how it handles deletes, edits, which cannot directly modify the collection and save it down to the database. Typically the developer must do that work rather than relying on it to be done automatically.

David Basarab
why can't it be rendered automatically atleast
deostroll
@deostroll - because it's stateless, you would be enforcing the control to make the automatica bind and that isn't part of the design.
JonH
A: 

A simple rule of thumb is that web apps versus win apps do not maintain state. That is why we have caching / session / cookies variables in web apps. If you could maintain state you wouldn't need to handle caching / sessioning out values that you may need across your entire site. You also wouldn't have to do checks for postbacks, etc.

It doesn't seem to make sense until you start working on web based applications on a daily basis for at least a few weeks. If you are a client win form developer you will notice this at first but you will learn how web apps work the more you work on them.

JonH