inline code VS page behind code

M

Mitch A

What are the advantages of using inline C# code in my aspx pages (with
inline I mean directly in the html between <% %>) versus code behind. It
seems messy and inelegant to have code in both places (IMHO, using inline
*and* code-behind is not object oriented and it generates less readable
code). Since code behind offers syntax highlighting, autocompletion, member
info, parameterinfo etc, is there any reason (design or other) I
couldn't/shouldn't write all code strictly using code-behind.
 
C

Chris R. Timmons

What are the advantages of using inline C# code in my aspx pages
(with inline I mean directly in the html between <% %>) versus
code behind. It seems messy and inelegant to have code in both
places (IMHO, using inline *and* code-behind is not object
oriented and it generates less readable code). Since code
behind offers syntax highlighting, autocompletion, member info,
parameterinfo etc, is there any reason (design or other) I
couldn't/shouldn't write all code strictly using code-behind.

Mitch,

I can't think of any. I use code-behind as much as possible. I only
use inline <%= %> tags if I need to insert some text at runtime in a
place where ASP.Net tags can't be used (or at least not easily used).

The <HEAD> section of an HTML page is a good example. What few
inline tags I do use I keep very short, and any actual calculations
are done in code-behind:

<HEAD>
<TITLE>
<%= Globals.Instance.GetCompanyName() %>
</TITLE>
<SCRIPT type="text/javascript"
src="../../scripts/clipboard.js">
</SCRIPT>
<%= Globals.Instance.GetCommentHeader() %>
<META name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<%= Globals.Instance.GetMetaTags() %>
<LINK rel="stylesheet" type="text/css" href="../../portal.css">
</HEAD>

Chris.
 
M

Mitch A

Thanks for responding Chris. Is it possible to manipulate ASPNET server
controls using only codebehind? For example, I have the following bit of
code currently embdedded in the HTML:

<asp:Repeater id="parentRepeater" runat="server">
<itemtemplate>
<table cellpadding=0 cellspacing=0 border=0 width=80% >
<td class="CandInfo"width="100%">
<%# DataBinder.Eval(Container.DataItem, "NM_FRST") %>
<td> </table>
</itemtemplate>
</asp:Repeater>


Other than the HTML tags identifying the control (<asp:Repeater>), can any
of this code be moved to the webform Page_Load event?
 
C

Chris R. Timmons

Thanks for responding Chris. Is it possible to manipulate
ASPNET server controls using only codebehind? For example, I
have the following bit of code currently embdedded in the HTML:

<asp:Repeater id="parentRepeater" runat="server">
<itemtemplate>
<table cellpadding=0 cellspacing=0 border=0 width=80% >
<td class="CandInfo"width="100%">
<%# DataBinder.Eval(Container.DataItem, "NM_FRST") %>
<td> </table>
</itemtemplate>
</asp:Repeater>


Other than the HTML tags identifying the control
(<asp:Repeater>), can any of this code be moved to the webform
Page_Load event?

Mitch,

I suppose it's technically possible to do so (see the help entry for
the System.Web.UI.WebControls.Repeater class). Personally, I don't
create controls in code unless I absolutely have to. I find it
easier to use the property editor to manipulate the control, and use
code-behind for everything else. The less code I have to actually
write, debug and maintain, the happier I am :).

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top