添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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.

Can you explain how binding later causes a shorter time in keeping the data in memory? I assume for a simple Collection kept in memory (as above, if someList is List<object>) there is no difference, but how does it work for something more complicated? Is there delayed evaluation? An example would be excellent. – JoshJordan Jun 11, 2009 at 22:26 Think of using an datareader as the source. If you bind right away, you'll iterate over the reader and have those contents in memory. If you wait for later in the page lifecycle, you hang on to it for less time. – Joel Coehoorn Jun 11, 2009 at 22:31 Thank you for the explanation, however I don't understand how does this reduce the amount of time you hold that connection active? The connection is open only when the ExecuteSelect() is called from the OnDataBinding event. When ExecuteSelect() finishes it calls dbConnection.Close(); and connection is no longer active. Am I missing something? – BornToCode Sep 10, 2014 at 14:23 For simple controls there is no difference. But some controls --epsecially certain third-party toolkits -- are designed to stream data directly into the response stream, so that only one record is ever in memory on the web server at a time. This is awesome for performance and scale, but binding them early opens a connection that will not be closed until the final render phase of the ASP.Net lifecycle. – Joel Coehoorn Feb 13, 2017 at 19:48

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.

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.

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.