Redirect user with ASP code in <body> ?

Discussion in 'ASP General' started by Noozer, Jul 25, 2005.

  1. Noozer

    Noozer Guest

    Is it possible to redirect a user to another web page using ASP code located
    in page body?

    I have some ASP code that runs after generating some output to the browser.
    It checks for a condition that can only be calculated using the values
    produced in the output, so redirecting the user before the <html> tag isn't
    possible without running the code twice (which I'd like to avoid.) - so
    RESPONSE.REDIRECT "url" won't work.

    Any suggestions?
    Noozer, Jul 25, 2005
    #1
    1. Advertising

  2. Set Response.Buffer = true

    Then you will be able to redirect before flushing the buffer output to the
    client.

    --
    Roji. P. Thomas
    Net Asset Management
    https://www.netassetmanagement.com


    "Noozer" <> wrote in message
    news:e51Fe.35518$s54.13194@pd7tw2no...
    > Is it possible to redirect a user to another web page using ASP code
    > located
    > in page body?
    >
    > I have some ASP code that runs after generating some output to the
    > browser.
    > It checks for a condition that can only be calculated using the values
    > produced in the output, so redirecting the user before the <html> tag
    > isn't
    > possible without running the code twice (which I'd like to avoid.) - so
    > RESPONSE.REDIRECT "url" won't work.
    >
    > Any suggestions?
    >
    >
    >
    Roji. P. Thomas, Jul 25, 2005
    #2
    1. Advertising

  3. > It checks for a condition that can only be calculated using the values
    > produced in the output


    I don't understand why there has to be any HTML output before these values
    are produced.

    >, so redirecting the user before the <html> tag isn't
    > possible without running the code twice (which I'd like to avoid.)


    Why do you have to run any code twice?

    Instead of saying:

    <%=x+y%>

    Just say:

    <%
    mySum = x + y
    %>

    Then later you can say,

    <%
    if mySum > 10 then
    response.redirect "place"
    else
    response.write "stuff"
    end if
    %>

    > - so RESPONSE.REDIRECT "url" won't work.
    >
    > Any suggestions?


    Yes, if you can't use more sensible logic on the server side, use
    client-side script.

    <% if mySum > 10 then %>
    <script>location.replace('place');</script>
    <% end if %>

    Now you don't have to alter the buffering behavior of the page, or fight
    with the server's settings, etc. I still don't understand why ASP script
    would ever have to be run twice...

    http://www.aspfaq.com/2262
    http://www.aspfaq.com/2011
    http://www.aspfaq.com/2217
    Aaron Bertrand [SQL Server MVP], Jul 25, 2005
    #3
  4. Noozer

    Noozer Guest

    > Yes, if you can't use more sensible logic on the server side, use
    > client-side script.
    >
    > <% if mySum > 10 then %>
    > <script>location.replace('place');</script>
    > <% end if %>
    >
    > Now you don't have to alter the buffering behavior of the page, or fight
    > with the server's settings, etc. I still don't understand why ASP script
    > would ever have to be run twice...


    Your entries:<br />
    <%
    for i = 1 to 20
    if Request.Form("Entry" & i)="" then
    Response.Redirect "EntryPage.asp"
    end if
    Response.Write "You entered " & request.form("Entry"&i) & " for #" & i &
    ".<br />" & vbcrlf
    next i
    %>
    Done!<br />
    Noozer, Jul 25, 2005
    #4
  5. > Your entries:<br />
    > <%
    > for i = 1 to 20
    > if Request.Form("Entry" & i)="" then
    > Response.Redirect "EntryPage.asp"
    > end if
    > Response.Write "You entered " & request.form("Entry"&i) & " for #" & i &
    > ".<br />" & vbcrlf
    > next i
    > %>
    > Done!<br />


    How about:

    <%
    for i = 1 to 20
    line = "You entered _e_ for #_i_.<br />" & vbcrlf

    e = Request.Form("Entry" & i)
    if e = "" then
    response.redirect "EntryPage.asp"
    response.end
    else
    output = output & replace(replace(line, "_e_", e),"_i_",i)
    end if
    next

    response.write output
    %>

    My main question, I guess re-worded, is, "why bother writing out 19 lines
    the user won't have time to see, if they didn't fill out the 20th entry?"
    And that leads to another question, why let them submit the form at all, if
    they are going to have to go back and start over? It seems you need to
    learn a little bit about usability... I would be pissed if you let me fill
    out 19 items, then sent me back to an empty form because you didn't bother
    *forcing* me to fill out the 20th item.

    A
    Aaron Bertrand [SQL Server MVP], Jul 25, 2005
    #5
  6. Noozer

    Noozer Guest

    It was an over simplified example... It's actually querying a database
    etc...

    "Aaron Bertrand [SQL Server MVP]" <> wrote in message
    news:...
    >> Your entries:<br />
    >> <%
    >> for i = 1 to 20
    >> if Request.Form("Entry" & i)="" then
    >> Response.Redirect "EntryPage.asp"
    >> end if
    >> Response.Write "You entered " & request.form("Entry"&i) & " for #" & i &
    >> ".<br />" & vbcrlf
    >> next i
    >> %>
    >> Done!<br />

    >
    > How about:
    >
    > <%
    > for i = 1 to 20
    > line = "You entered _e_ for #_i_.<br />" & vbcrlf
    >
    > e = Request.Form("Entry" & i)
    > if e = "" then
    > response.redirect "EntryPage.asp"
    > response.end
    > else
    > output = output & replace(replace(line, "_e_", e),"_i_",i)
    > end if
    > next
    >
    > response.write output
    > %>
    >
    > My main question, I guess re-worded, is, "why bother writing out 19 lines
    > the user won't have time to see, if they didn't fill out the 20th entry?"
    > And that leads to another question, why let them submit the form at all,
    > if they are going to have to go back and start over? It seems you need to
    > learn a little bit about usability... I would be pissed if you let me fill
    > out 19 items, then sent me back to an empty form because you didn't bother
    > *forcing* me to fill out the 20th item.
    >
    > A
    >
    Noozer, Jul 26, 2005
    #6
  7. > It was an over simplified example... It's actually querying a database
    > etc...


    And...? How does that change anything?
    Aaron Bertrand [SQL Server MVP], Jul 26, 2005
    #7
    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. =?Utf-8?B?VHJveQ==?=

    Response.redirect does not redirect from .aspx page

    =?Utf-8?B?VHJveQ==?=, Oct 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    5,213
    Srowe1975
    Oct 15, 2008
  2. Sal
    Replies:
    1
    Views:
    370
  3. shane
    Replies:
    4
    Views:
    159
    Alan J. Flavell
    Jul 12, 2003
  4. London Boy
    Replies:
    2
    Views:
    313
    Keith Bowes
    Jan 12, 2004
  5. Replies:
    5
    Views:
    196
    A. Sinan Unur
    Sep 8, 2005
Loading...

Share This Page