JoshJordan
JoshJordan

Reputation: 12975

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?

Upvotes: 22

Views: 26567

Answers (4)

gt2
gt2

Reputation: 11

Try this:

if (GridView1.EditIndex == e.Row.RowIndex)
{              
    TextBox t2 = (TextBox)e.Row.FindControl("TextBox2");
    DateTime dt2;
    if (DateTime.TryParse(t2.Text, out dt2))
    {
        t2.Text = dt2.ToString("yyyy-MM-dd");
    }
}

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

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.

Upvotes: 18

Emad-ud-deen
Emad-ud-deen

Reputation: 4874

I noticed that without using DataBind() that nothing will be displayed in my GridView so I always include it as shown in this section of code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' TableAdapter object.
    ' Provide communication between this application and the database.
    '-----------------------------------------------------------------
    Dim suppliersAdapter As New SuppliersTableAdapter

    ' Get the data from the TableAdapter into the GridView.
    '------------------------------------------------------
    GridView1.DataSource = suppliersAdapter.GetSuppliers()

    ' Display the result set from the TableAdapter in the GridView.
    '--------------------------------------------------------------
    GridView1.DataBind()
End Sub

Please forgive the extra commenting as I'm also still learning ASP.Net as well and the comments will help me learn better "what and why" to use certain statements.

Upvotes: 0

Matthew Jones
Matthew Jones

Reputation: 26190

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.

Upvotes: 2

Related Questions