473,378 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Why use <%= %> ?

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 Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
12 1165
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:gO**************@nospamthankyou.spam...
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 Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
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 Silver" <al*********@nospam.thanx> wrote in message
news:gO**************@nospamthankyou.spam...
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 Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #3
>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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #4
>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.
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
Eliyahu

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:gO**************@nospamthankyou.spam...
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 Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #5
What, in the body tag, are you trying to set programmatically? There are
almost always more than 1 way to do things ;)

Mythran
Nov 19 '05 #6
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:Vr**************@nospamthankyou.spam...
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 Ithought just use <base id="x" runat="server" /> and declare it as an
HtmlGenericControl...but it turns out <base> CAN'T have a closing tag, andall 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

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #7
>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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #8
>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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #9
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.

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:Bs**************@nospamthankyou.spam...
There are controls which let you render text or even html inside a tag, butI 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.

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #10
>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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #11
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.

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:gO**************@nospamthankyou.spam...
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 Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #12
>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!!
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:gO**************@nospamthankyou.spam...
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 Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Francesco Moi | last post by:
Hello. I'm trying to build a RSS feed for my website. It starts: ----------------//--------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE rss PUBLIC "-//Netscape...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
0
by: Arne Schirmacher | last post by:
I want to display a MySQL database field that can contain HTML markup. If I use <esql:get-string> then I get all of the database field, but all tags are escaped which is not what I want. If I use...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
2
by: Francesco Moi | last post by:
Hello. I designed a form to edit some DataBase's fields. But some of these fields contain '&lt;' and '&gt;' characters. And these characters are '<' and '>' in HTML. So if want to edit these...
34
by: Mark Moore | last post by:
It looks like there's a pretty serious CSS bug in IE6 (v6.0.2800.1106). The HTML below is validated STRICT HTML 4.01 and renders as I would expect in Opera, FrontPage, and Netscape. For some...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
6
by: tentstitcher | last post by:
Hi all: I have a source xml document with an element of type string. This element contains something like the following: <stringData> &lt;Header&gt; &lt;Body&gt; </stringData> I would like to apply an...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.