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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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.

Method 4

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");
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x