Code in the HTML, bad idea?

M

Michael

What is the general opinion of having code in the html section of an asp.net
page. For example..

<img src="<%=myImageString%>" />

I see something like that done many times in examples, but I'm wondering if
this is a good practice, or something to be avoided. If one of the goals of
asp.net was to separate html from code, this just seems to be going back to
the old "asp" ways.
 
H

Hermit Dave

Michael,

There are times when its easier just to use embedding !!! specially when you
are dealing with html controls.
Then there are times when you could like to see how its going to be instead
of manipulating one param
Take the example of a data grid... you can customise what columns get shown
and whether they are directly bound to the datasource or whether they are
template based.
yes you can do most of the things from code behind but code behind doesnt
emit things at design time.... unless of course you start writing custom
controls for just about everything.

So i guess its a personal choice really... depends on whether you like to
keep it uniform... or you could like to take extra effort in order to keep
it extra clean,

Regards,

HD
 
S

Scott M.

I wouldn't use what you are showing at all, ever. That's the "classic asp"
way to do it and it is not as efficient as .NET techniques.

With
 
P

Patrice Scribe

Something that could be perhaps acceptable is :
- if no programmatic id, you can't access it from your code behind. Inline
code is allowed.
- if a value is set once but never changed (such as a common function that
would retrieve localized strings from a database), inline code allowed.

If the value can change depending on the events (for example you change the
image if you meet some other condition at load time or after a postback or
whatever else) keep it in code, inline code to be avoided.

Basically the idea would be to allow inline code for one time initialization
expressions and to avoid inline code when you have some further processing
for this property... Of course always a matter of personal taste...

Patrice
 
K

Kevin Spencer

The basic idea behind the ASP.Net CodeBehind model is that it is difficult
to both develop and especially maintain the software when interface HTML and
executable server-side code are mixed up together. I agree wholeheartedly
with this philosophy, as I have had to work on far more than my share of
poorly-organized ASP code. Good coding practice dictates that code should be
well-optimized, well-organized, and well-conceived. The placement of the
code in the HTML would have very little effect on the actual execution of
the Page. However, it might have a great effect on how long it would take
another developer to make any changes in the Page in the future.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

JiangZemin

I dont think its considered good practice and i think anyone would tell you
that its preferable to put as much code in code-behind as possible, but
realistically speaking sometimes its just easier/faster (in the short term)
to put stuff like this directly in html.
i believe one of the benefits of this quickie approach is that it doesnt
require you to recompile from VS.NET. you can just edit this file in
Notepad or something and copy the new file onto the server.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top