3

This is more of a question of preferred methods and the benefits of either. When developing some data-driven controls and pages with things like Repeaters, I'm at an impass.

Do I create ASP.NET tags (Label, etc.), and bind them in-line?

<asp:Label ID="companyName" runat="server" Text="<%# ((BillToCustomer)Container.DataItem).CompanyName %>" />

Or is it better to leave the .aspx/.ascx file for plain markup and put all the logic in the code-behind?

<asp:Label ID="companyName" runat="server" ondatabinding="companyName_DataBinding"/>

protected void legCustomerName_DataBinding(object sender, EventArgs e)
{
    ((Label)sender).Text = ((BillToCustomer)Page.GetDataItem()).CompanyName;
}

I can argue myself to death over either option, but I want to be know all the facts and opinions before I commit to one method.

The only resource I was able to find discussing this ( OnDataBinding vs Inline: pros, cons and overhead ) didn't have many answers with convincing details.

5
  • 1
    I always found that it was easier to put as little inline code into the page as possible, so as to keep all code grouped in code-behind (where it belongs in my opinion). That way I found I had to search less for the code later. Also, there was no intellisense support on the page (but could be wrong, as ASP.NET was a long time ago for me and it's getting quite hazy...), which made inline more difficult to get right first go.
    – yu_ominae
    Nov 14, 2012 at 2:59
  • What is your preferred method? If it is a straight EVAL, I'll just use code front. But sometimes more logic is needed, and code behind is best/only way to go. Nov 14, 2012 at 3:08
  • @user643192 - It does have some Intellisense (VS2010 at least), and I'm sort of leaning towards that same method - but a binding method for each control is looking very messy on certain pages Nov 14, 2012 at 13:32
  • @MikeSmithDev - that's what I'm trying to decide, my preferred method, and getting input on others', or if there's a better way / guidelines. If there's more logic involved you could always use a method to calculate what's needed and call that on the code front page. Nov 14, 2012 at 13:33
  • @AnthonyAziz Ah, well, proof that my memory is starting to fail Yes, I could see that getting quite messy if you had to databind many controls. It's personal preference of course, but I think that it's best to keep code in codebehind. You get many methods if you have many controls, but then you also get a lot of html markup and that can get messy too.
    – yu_ominae
    Nov 15, 2012 at 0:13

2 Answers 2

1

After tossing this around for a while and bouncing off a coworker, I think I've come to the conclusion that I'm better off keeping my value binding in the code behind, using the Repeater's ItemDataBound event.

The markup is strictly comprised of web controls with ID's - Labels, mostly, in my cases. If the markup/design and code-behind responsibilities were split, the designer would be free to move the markup around as needed, and the developer would have control over setting values. The designer wouldn't be concerned over the data binding expressions.

This also allows for some neater and more intuitive code, instead of trying to cram everything into one expression.

I'm going to accept this as the answer as this is the solution I ended up using, but I am certainly still interested in further discussion.

1
  • It should be noted that if you do both, the inline version is executed first, so if you change the same field in both places the code behind will override the inline databinding value.
    – jmoreno
    Aug 7, 2021 at 21:13
0

These two are precisely the same. Your "code-behind" version is the same thing that the ASP.NET page compiles to.

2
  • Yes, I understand that's the case. This is mostly a question of code style and the advantages of one method over the other in the long run for developing large-scale web applications. Nov 14, 2012 at 13:30
  • I don't know that there is any clear advantage one way or the other. It depends on things like designer support and your style. If you use the designer, and use controls with good designer support for databinding, then the markup/designer method will be faster. If you find yourself fighting the designer, then use the codebehind method. Nov 14, 2012 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.