By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,356 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,356 IT Pros & Developers. It's quick & easy.

Code in the HTML, bad idea?

P: n/a
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.

Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"Michael" <raterus@localhost> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #2

P: n/a
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
"Michael" <raterus@localhost> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #3

P: n/a
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

--

"Michael" <raterus@localhost> a écrit dans le message de
news:OH**************@tk2msftngp13.phx.gbl...
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.


Nov 18 '05 #4

P: n/a
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.

"Patrice Scribe" <no****@nowhere.com> wrote in message
news:Or**************@TK2MSFTNGP12.phx.gbl...
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

--

"Michael" <raterus@localhost> a écrit dans le message de
news:OH**************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #5

P: n/a
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.

"Michael" <raterus@localhost> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.