What’s the difference between these code samples? Which approach is right?
<asp:Label ID="lblShorName" runat="server" Text="<%#Customer.ShorName%>" />
lblShorName.DataBind();
and
lblShorName.Text = Customer.ShorName;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There isn’t much of a difference that I know of (though I’ll be interested in other people’s answers to correct me if I’m wrong on that). It’s just a matter of coding style and preference.
Personally, I prefer the latter. I feel that it’s cleaner and separates the markup from the functionality which drives the markup. But that’s just me.
(I also tend to prefer not using data binding where I don’t feel I need to. But, again, it’s a preference of how you want to use the tooling that’s provided. For example, in an ASP.NET MVC view I’m more likely to write a loop and output HTML within that loop than I am to use any kind of repeater or grid control and bind data to it. Just personal preference.)
A lot of it also comes down to where in your application you want to perform these actions. The former example keeps it on the page, whereas the latter example can be wrapped in conditionals, re-factored into another method, etc. If it’s possible that the value in question isn’t always going to come from
Customer.ShortNamethen I’d go with the latter example to add that additional logic around it.