Wednesday, July 29, 2009

Inline Vs. Code Behind

StackOverflow has this discussion on some code being inline in the aspx page versus it getting into the code behind file.

Well, it made me start thinking. Thinking about it, I used to hate code getting into aspx pages with my colleagues. It led into code getting scattered and ended up losing the control of the code. Somehow, code behind makes it look more natural.

However, in case of a gridview - some people get more code into code behind

<asp:Literal ID="litExample" runat="server"

OnDataBinding="litExample_DataBinding" />


protected void litExample_DataBinding(object sender, System.EventArgs e)

{

Literal lit = (Literal)(sender);

lit.Text = string.Format("{1} - {2}",

Eval("ExampleField").ToString(),

Eval("ExampleField2").ToString());

}


There will be no performance difference on either type of the code. It's more to do with the best practices and to improve the readability of the code.


In this case, I like to keep the code in the aspx page as the code here is more declarative and design oriented.


    Text='<%# Eval("ExampleField").ToString() %>' />



0 comments: