Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the DataBind() method necessary?

Simple question, I guess.

For a long time I've blindly followed a (supposedly) common pattern when programmatically databinding my ASP.NET controls. Namely:

gridView1.DataSource = someList;
gridView1.DataBind();

However, if I were setting my GridView to bind to a DataSource control via the DataSourceID property, the call to DataBind() is unnecessary. Namely:

gridView1.DataSourceID = LinqDataSource1;

is sufficient.

Furthermore, if you try to set the DataSource property in ASPX markup, you are greeted with the following:

You cannot set the DataSource property declaratively.

I assume these are related, but I am still stumped as to why DataBind() is necessary. The difference between DataSource and DataSourceID is secondary - I can understand some magic taking place there. The real question is why doesn't the DataSource propery setter cause databinding automatically? Are there any scenarios in which we want to set the DataSource but not bind to it?

like image 653
JoshJordan Avatar asked Jun 11 '09 22:06

JoshJordan


People also ask

What does Page DataBind do?

In ASP.NET, you can bind controls individually (i.e. GridView1. DataBind() ) or you can call Page. DataBind() to bind all controls on the page.

What is DataBind?

DataBind() Binds a data source to the invoked server control and all its child controls. DataBind(Boolean) Binds a data source to the invoked server control and all its child controls with an option to raise the DataBinding event.

What is DataBind in Ado net?

When you only need to have a control display a single value, like that of a TextBox control, this limited usage is referred to as "simple data binding." Simple data binding is the ability to bind a control to a single data element (such as a value in a column in a DataSet table).


2 Answers

In ASP.Net, it's often important to have certain data available and ready at certain points in the page life cycle, and not before. For example, you may need to bind to a drop down list early to allow setting the selected index on that list later. Or you might want to wait a bit to bind that large grid to reduce the amount of time you hold that connection active/keep the data in memory.

Having you explicitly call the .DataBind() method makes it possible to support scenarios at both ends of the spectrum.

like image 120
Joel Coehoorn Avatar answered Oct 22 '22 09:10

Joel Coehoorn


DataSource is a property of the BaseDataBoundControl class. DataSourceID is a property of the DataBoundControl class, which inherits from BaseDataBoundControl and did not exist before ASP.NET 2.0.

Since DataBoundControl is explicitly for displaying data in a list or tabular form, and BaseDataBoundControl cannot make that assumption, binding is not automatic when DataSource is set because the type of control may not match the structure of the data.

Of course, this is all just a guess based on the MSDN documentation, so I could be wrong.

like image 25
Matthew Jones Avatar answered Oct 22 '22 09:10

Matthew Jones