Best practices for using <%...%> in ASP.Net

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I started coding with ASP.Net a little while ago and am trying to find the
right balance of when to use classic ASP (<%...%>) style scripting. I
originally tried to code all server-side logic with the code-behind page,
with certain databinding exceptions (repeater control etc.), but now am
reconsidering that approach.

For example, I want to show different columns in a list on a form depending
on what filters the user selects. Lets say the data is baseball players and
I want to show pitching stats if the list is for pitchers vs batting stats
for everyone else. I essentially have 3 options (I'm sure there are more
but I can only see 3).

1) Have multiple WebForms for each column set to be displayed (Pitching and
Batting).

2) Use a server-side table and do all the logic and html building in the
code-behind page.

3) Use a repeater (or similar control) but do classic <%...%> style
scripting to determine what columns show.

I'm guessing that 2) is the best because it is early bound and handles the
logic on the same WebForm, but is there a better way? When is it considered
ok or desirable to use <%...%>?

Thanks in advance,
Dan
 
Generally speaking, you want to keep your logic out of the Template.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Personally, I prefer to stay away from <% %> as much as possible. However,
when using datalists, or other similar controls, this is sometimes
unavoidable. But mainly, staying away from Response.Write, and other type of
things in those scripts is the real idea.

If you use something like a datagrid, that is really easy to use without
knowing what columns you have ahead of time. I think you definitely would
want to stay away from the situation where you have a separate webform for
every search type combination.
 
Dan,

It's not a problem to use the <% and %> delimiters. However, it seems that
you are asking not explictly about that, but about placing ASP.NET code
inline with your HTML as we did with legacy ASP. That is discouraged
because ASP.NET is not a scripting language and development should not be
approached in this way. Whether your code is in a code-behind file or
inline, you always want to write your code to the model in which ASP.NET
was designed, and that means coding to events and method calls.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support Engineer
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
 
Back
Top