Why use <%= %> ?

  • Thread starter Thread starter Alan Silver
  • Start date Start date
A

Alan Silver

Hello,

Newbie alert, so please be patient <g>

I ASP Classic, we were used to using tricks like ...

<%=strName%>

to insert dynamic content into an HTML block. In ASP.NET, you have
various controls like the label that seem to do the same thing, but with
much more power and flexibility. If so, is there any reason to use <%=
%> at all?

I only ask as I am reading ASP.NET Unleashed, and he shows an example of
using this. I couldn't see why he didn't just use a control. Granted his
example was something like ...

<body bgcolor="<%=strColour%>">

where he was including an attribute for a tag, rather than a whole tag,
but he already mentioned a generic control that just produced the basic
text rather than a tag, so why not use that?

TIA
 
Alan:
You are right, there's almost never a good reason to prefer <%= %> over
using controls. True <%= %> might be nanoseconds faster, but you pay the
price (about 10x over) with maintenance and bugs. Hopefully the book was
just showing that you COULD do it. There might be very odd occasions were
you want to. Someone recently asked how to create a <base> tag...at ifrst I
thought just use <base id="x" runat="server" /> and declare it as an
HtmlGenericControl...but it turns out <base> CAN'T have a closing tag, and
all server/html controls render a closing tag. I provided the guy with a
server control which overwrote the closing tag behaviour, but using <%= %>
might have been an easier solution for him to use...There have been other,
although I can probably count them on a single hand, occasions...

Karl
 
Good example. There is nothing in asp.net representing <body> tag, so use of
<%... is appropriate. You can replace <body> with a generic control (then
you will end up with two <body>). Also you might prefer keeping your old asp
style.

Eliyahu
 
Alan:
You are right, there's almost never a good reason to prefer <%= %> over
using controls. True <%= %> might be nanoseconds faster, but you pay the
price (about 10x over) with maintenance and bugs. Hopefully the book was
just showing that you COULD do it.

OK, I guessed it wasn't necessary (usually), but it's nice tohave it
confirmed.
There might be very odd occasions were
you want to. Someone recently asked how to create a <base> tag...at ifrst I
thought just use <base id="x" runat="server" /> and declare it as an
HtmlGenericControl...but it turns out <base> CAN'T have a closing tag, and
all server/html controls render a closing tag. I provided the guy with a
server control which overwrote the closing tag behaviour, but using <%= %>
might have been an easier solution for him to use...There have been other,
although I can probably count them on a single hand, occasions...

OK, forgive the ignorance as I'm *really* new at this, but I think I
read that one of the generic controls just produces pretty much what you
put in, which could be plain text. If I'm right, then can't you use that
instead? Or can't you use a control inside a tag? Just occurred to me as
I'm typing that this might not work.

Thanks
 
Good example. There is nothing in asp.net representing said:
<%... is appropriate. You can replace <body> with a generic control (then
you will end up with two <body>). Also you might prefer keeping your old asp
style.

I'm trying hard to learn ASP.NET as it is meant to be, not as a step
from ASP Classic ;-) I'm trying to get it right from the start, not keep
old ideas hanging over. Not easy ;-)

Thanks
 
What, in the body tag, are you trying to set programmatically? There are
almost always more than 1 way to do things ;)

Mythran
 
There are controls which let you render text or even html inside a tag, but
I don't think there are any that let you define the attributes via
strings...even if they did, it would be the same as using <%= %> as you
wouldn't be able to program against it.

Karl
 
There are controls which let you render text or even html inside a tag, but
I don't think there are any that let you define the attributes via
strings...even if they did, it would be the same as using <%= %> as you
wouldn't be able to program against it.

Why not? If you had a control that produced an attribute, you could
program against it. I can see that it would be the same as using <%=%>,
except that the code would be written in a more ASP.NET way, using
controls and setting them in the code block, rather than inserting code
in the HTML section, which is more of a Classic ASP approach.
 
What, in the body tag, are you trying to set programmatically? There are
almost always more than 1 way to do things ;)

The example I saw in the book was using <%=%> to set the bgcolor
attribute of the body tag. It was a simple example where you pick a
colour from a set of radio buttons, and the page is set to use that
colour when you post back.
 
Well, if you did:

myControl.AttributeString = "href='somevalue.html' target='_blank'"

you can't program against the href and target, in other words you couldn't
go

myControl.href = 'xxx' or
myControl.target = 'yyy'

they wouldn't be properties..just a string to render out..thankfully nothing
like that exists. You can always do control.Attributes.Add("href",
"blah.html") but that didn't solve the original problem of no closing
tag...

the code might not be in the HTML, but really you haven't solved the
uglyness, just moved it.
 
Well, if you did:
myControl.AttributeString = "href='somevalue.html' target='_blank'"

you can't program against the href and target, in other words you couldn't
go

myControl.href = 'xxx' or
myControl.target = 'yyy'

they wouldn't be properties..just a string to render out..thankfully nothing
like that exists. You can always do control.Attributes.Add("href",
"blah.html") but that didn't solve the original problem of no closing
tag...

the code might not be in the HTML, but really you haven't solved the
uglyness, just moved it.

OK, maybe I misunderstood. I thought you meant that if you had a control
that rendered attributes, you could do something like ...

<a href="<mycontrol id="fred">">hello</a>

and then in code ...

mycontrol.fred = "http://www.blah.com"

but as I type it now, it just doesn't look right. I guess I see where
you are coming from. OK, so maybe there is a use for it!! Not so common,
but genuine.

Thanks for the reply
 
both approaches are possible and at times necessary.
In the case you cited, there is not an explicit control that represents the
BODY element in the document. everything before the FORM element is a
single Literal Control. So in that case, you have the options of either (1)
using an inline snippet or (2) doing a find/replace for "<body>"

Personally, I'd rather do the inline code.
 
both approaches are possible and at times necessary.
In the case you cited, there is not an explicit control that represents the
BODY element in the document. everything before the FORM element is a
single Literal Control. So in that case, you have the options of either (1)
using an inline snippet or (2) doing a find/replace for "<body>"

Personally, I'd rather do the inline code.

Yup, sounds sensible to me. Thanks for the clarification. Being somewhat
new at this, I'm still working out what's what!!
 
Back
Top