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

Discussion in 'ASP .Net' started by samuelberthelot@googlemail.com, Feb 23, 2006.

  1. Guest

    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.
    , Feb 23, 2006
    #1
    1. Advertising

  2. Hugo Flores Guest

    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?
    Hugo Flores, Feb 23, 2006
    #2
    1. Advertising

  3. Guest

    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. :(
    , Feb 23, 2006
    #3
  4. Hugo Flores Guest

    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.
    Hugo Flores, Feb 28, 2006
    #4
  5. Guest

    I've read that in a book that using <% %> in the aspx page instead of
    <script/> is not good. It is obsolete...
    , Mar 1, 2006
    #5
  6. Hugo Flores Guest

    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
    Hugo Flores, Mar 1, 2006
    #6
  7. 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.
    --
    Christopher A. Reed
    "The oxen are slow, but the earth is patient."

    <> wrote in message
    news:...
    > 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.
    >
    Christopher Reed, Mar 2, 2006
    #7
  8. Alan Silver Guest

    ><> wrote in message
    >news:...
    >> Hi,
    >> I have the following in my asp page:
    >>
    >> <% response.write(Header) %>
    >>
    >> where Header contains HTML markup such ass <html> <body> ....


    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.

    >> 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.


    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.

    >> I've tried :
    >>
    >> <script runat=server>
    >> response.write(Header)
    >> </script>
    >>
    >> but that wouldn't work. How can i do it ?


    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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Mar 2, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    2
    Views:
    7,980
    Charles A. Lackman
    Jan 11, 2006
  2. Matthew Wieder

    Response.Write and Response.Redirect

    Matthew Wieder, Jul 17, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    682
    Kevin Spencer
    Jul 17, 2003
  3. Sehboo
    Replies:
    3
    Views:
    8,767
    Sehboo
    Dec 10, 2003
  4. Replies:
    1
    Views:
    822
  5. Niall Porter
    Replies:
    5
    Views:
    666
    Aaron [SQL Server MVP]
    Nov 30, 2004
Loading...

Share This Page