asp.net equivalent of <% response.write("") %>

S

samuelberthelot

Hi,
I have the following in my asp page:

<% response.write(Header) %>

where Header contains HTML markup such ass <html> <body> ....

I must write the code in the aspx file and not in the code behind so
that the markup are generated at correct places in html stream.

I've tried :

<script runat=server>
response.write(Header)
</script>

but that wouldn't work. How can i do it ?

Thank you.
 
H

Hugo Flores

You should be able to do it in the same way as you do it now with <%
response.write(Header) %>.
The script section you tried should also work, even though I've never
used it myself as I always use the <% %>, if I have to code something
in the aspx file.
So maybe the problem is that you are not getting the Header right,
where are you getting the Header from?
 
S

samuelberthelot

Yes I know I can do it with <% %> and it works fine actually.
But I was told this is not good to do this in asp.net anymore.
I should use the <script> markup but if I do then it wouldn't work for
some reason. :(
 
H

Hugo Flores

It's not good to do that in .net anymore????
Who told you that?
It's the same thing as doing it on the script block.
 
S

samuelberthelot

I've read that in a book that using <% %> in the aspx page instead of
<script/> is not good. It is obsolete...
 
H

Hugo Flores

I never heard that before, but well if you don't want to use <% %>
which works, then I think that you would have to use a control that
parses the html and place it exactly where you want the html to be
placed (if that's even possible, because I never had to do this).
Because it seems that if you want to use the script block, the Response
object, or any other for that matter need to be written under any
method or property.
I think, if it fits your need you should go with what works for your
solution, no matter how "obsolete" it is. And if it's obsolete how come
directives still use the <% %>.
Notice that I'm not encouraging the use of <% %> in any way. But in
your particular case, that's the best way to go. In any other case,
where you just want to write something into a page, is better to use
controls and place it under their corresponding property.

HTH
 
C

Christopher Reed

You should avoid the <% %> construct any more because it doesn't conform
with the ASP.NET programming model. There are instances within controls
like Repeater and DataList where it is appropriate to use <%# %> when you're
trying to add database table values to a control. But to use the <%= %>
construct that is from classic ASP is no longer appropriate with the new
programming model. Look at the MSDN documentation for more information.
 
A

Alan Silver

We discussed this already in another thread. It was pointed out to you
that this isn't a good idea. You shouldn't store the HTML in the
database, rather store the attributes (eg page title, meta description,
etc) in the database and populate server controls.

That doesn't make sense. Populating in the aspx file does not give you
any more control over where the HTML is generated, and it mixes code
with presentation. Code-behind gives you full control over where the
HTML (or any other content) is placed.

You are thinking in a classic ASP way, and that is hindering your
attempts.

Furthermore, as already explained, this will *not* allow you to mark the
HTML you're writing as runat="server" anyway, so your attempts to use
themes with such a header will simply not work, no matter how many times
you ask the question.

You could read the suggestions you've already been given. What you show
above will definitely work, it will insert the HTML at the specified
place. However, having a Literal control in the aspx file and setting
its Text property in the code-behind works just as well, but has major
advantages of separating the code from the presentation.

For example, if your HTML in the aspx file looks like this...

<head runat="server">
<title><asp:Literal id=litTitle" runat="server" /></title>
</head>

then you can set the title of the page from code-behind *and* have
themes working.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,131
Latest member
IsiahLiebe
Top