Beginner's question - How do you redirect a user to another page

R

RobG

Hi there,

I have a form and when the user clicks the submit button, I want the handler
for that on the server side to redirect ("post") the form to a URL at
another site (not my own). It should appear to the user's browser as if the
form's "action" attribute were set to that site to begin with (so the start
page at that site will have access to the hidden fields on the form - it's
expecting them). Thanks in advance.
 
M

Mark Rae [MVP]

I have a form and when the user clicks the submit button, I want the
handler for that on the server side to redirect ("post") the form to a URL
at another site (not my own). It should appear to the user's browser as if
the form's "action" attribute were set to that site to begin with (so the
start page at that site will have access to the hidden fields on the
form - it's expecting them).

The simplest way would be to use an HTML form instead of an ASP.NET form
e.g.

<form method="post" action="http://www.theotherwebsite.com">

</form>

Of course, that will mean that you can't use any webcontrols at all but,
since you're not posting back, I'm guessing that probably won't matter too
much...
 
R

RobG

I have a form and when the user clicks the submit button, I want the
The simplest way would be to use an HTML form instead of an ASP.NET form
e.g.

<form method="post" action="http://www.theotherwebsite.com">

</form>

Of course, that will mean that you can't use any webcontrols at all but,
since you're not posting back, I'm guessing that probably won't matter too
much...

Thanks for the feedback. Unfortunately, I need the submit button handler to
set a hidden field based on the value of one of the form's controls (ideally
I'd like to dynamically create his hidden field on-the-fly - not sure how
though). Once set, it can then post to the real the URL but again, it should
appear as if the user went directly there to begin with. Do you know how to
pull this off? Thanks again.
 
M

Mark Rae [MVP]

Thanks for the feedback. Unfortunately, I need the submit button handler
to set a hidden field based on the value of one of the form's controls

Is there a reason that can't you use JavaScript for that...?
 
R

RobG

Thanks for the feedback. Unfortunately, I need the submit button handler
Is there a reason that can't you use JavaScript for that...?

I need to encrypt the data on the server side and assign it to the hidden
field before posting.
 
M

Mark Rae [MVP]

I need to encrypt the data on the server side and assign it to the hidden
field before posting.

Hmm - OK...

In which case, I can only think of the following:

1) Keep your existing page which does the postback to encrypt the data

2) Add an <asp:Literal> control to the page

3) As part of the postback, use a StringBuilder to create the entire text of
an HTML form and add that to the Literal control

4) Finally, inject a piece of JavaScript to submit the dynamically created
form

This is pretty messy, but it's not unheard of... E.g. this is the way people
typically integrate PayPal into their ASP.NET apps...
 
R

RobG

Hmm - OK...
In which case, I can only think of the following:

1) Keep your existing page which does the postback to encrypt the data

2) Add an <asp:Literal> control to the page

3) As part of the postback, use a StringBuilder to create the entire text
of an HTML form and add that to the Literal control

4) Finally, inject a piece of JavaScript to submit the dynamically created
form

This is pretty messy, but it's not unheard of... E.g. this is the way
people typically integrate PayPal into their ASP.NET apps...

Thanks very much. I'll be looking into this immediately. Note that I also
just came across the following which looks promising:

http://www.c-sharpcorner.com/UploadFile/desaijm/ASP.NetPostURL11282005005516AM/ASP.NetPostURL.aspx

I have yet to look at it in detail but the author states (as you yourself do
indirectly) that ASP.NET provides no clean way to do this. That comes as a
surprise to me but so long as it can be done somehow (even using unwieldy
techniques) I'm grateful. Thanks again.
 
M

Mark Rae [MVP]

This is the updated link on the author's site:

http://www.jigar.net/articles/viewhtmlcontent78.aspx

That's pretty much what I was suggesting, though I think the author is
perhaps being a little too clever for his own good...

Specifically, I try to avoid writing directly to the Response stream because
you can't always guarantee where things go... Having said that, the first
thing that the author's code does is to clear the Response stream, so this
is probably OK...
 
R

RobG

That's pretty much what I was suggesting, though I think the author is
perhaps being a little too clever for his own good...

Specifically, I try to avoid writing directly to the Response stream
because you can't always guarantee where things go... Having said that,
the first thing that the author's code does is to clear the Response
stream, so this is probably OK...

Thanks for the insight. I'm now investigating both his technique and yours
which are similar as you said. At least I can now search for yours using
appropritae keywords including "PayPal". I'm trying to do this as safely as
I can which means (if possible) generating the exact same page but with my
hidden field now filled in. The "onload" attribute seems to be the key to
the whole thing (which makes sense) so I should be able to unravel things
now. Thanks again for your assistance (greatly appreciated).
 
C

Cowboy \(Gregory A. Beamer\)

You can set the form tag to point anywhere you like, as Mark has pointed
out.

One potential issue, however, is the browser may alert the user of your
action, as you are working cross site. In addition, if the form at the other
side can be set up to not allow what you are attempting (you have this
worked out already with the guys on the other side?).
 
R

RobG

You can set the form tag to point anywhere you like, as Mark has pointed
out.

One potential issue, however, is the browser may alert the user of your
action, as you are working cross site. In addition, if the form at the
other side can be set up to not allow what you are attempting (you have
this worked out already with the guys on the other side?).

I actually got it working with Mark's suggestion about using a "Literal"
class. I'm still researching the issue however. For instance, I'm currently
trying to figure out how (or if) the "onload" attribute can be dynamically
added to the page's "body" tag in my submit button's event handler. By doing
this and then updating the form's "action" attribute to the new URL (is this
even legal in ASP.NET?), I can then immediately submit the form to the
latter URL as soon as the page is returned to the client (of course if the
user returns to the same page it needs to be properly restored again). At
this point I'm simply trying to figure out how to find the "body" tag in my
submit button's event handler (or any other element in the page for that
matter). Do you know to do this off-hand? Thanks for your assistance.
 
L

Lars

Hi

One way is to do it from the .cs or .vs file as shown below.

Responce.Redirect("page.aspx");

This is rather useful if you are dealing with different roles. In the
Page_Load function you can redirect the user depending on the users right to
access. For example.

Users who hasn't logged in are redirected to Login.aspx

Users who are logged in and are a member of "Basic User" can view data from
the database and are redirected to View.aspx.

User that has Administrators prevelidge can edit the databasea nd are
redirected to Edit.aspx


As shown below

protected void Page_Load(object sender, EventArgs e)

{

if (User.Identity.IsAuthenticated == false)

{

Response.Redirect("Login.aspx");

}

else if (User.IsInRole("Administrator") == false)

{

Response.Redirect("Edit.aspx");

}

else if (User.IsInRole("Basic User") == false)

{

Response.Redirect("View.aspx");

}

}








Lars
 
M

Mark Rae [MVP]

For instance, I'm currently trying to figure out how (or if) the "onload"
attribute can be dynamically added to the page's "body" tag in my submit
button's event handler. By doing this and then updating the form's
"action" attribute to the new URL (is this even legal in ASP.NET?), I can
then immediately submit the form to the latter URL as soon as the page is
returned to the client

Not necessary... As I mentioned, all you need is a single line of JavaScript
after the dynamic form which will submit it:

<form id="MySecondForm" method="post" action="http://www.otherwebsite.com">
<!-- rest of second form -->
</form>
<script type="text/javascript">
document.forms['MySecondForm'].submit();
</script>
 
R

RobG

RobG said:
For instance, I'm currently trying to figure out how (or if) the "onload"
attribute can be dynamically added to the page's "body" tag in my submit
button's event handler. By doing this and then updating the form's
"action" attribute to the new URL (is this even legal in ASP.NET?), I can
then immediately submit the form to the latter URL as soon as the page is
returned to the client

Not necessary... As I mentioned, all you need is a single line of
JavaScript after the dynamic form which will submit it:

<form id="MySecondForm" method="post"
action="http://www.otherwebsite.com">
<!-- rest of second form -->
</form>
<script type="text/javascript">
document.forms['MySecondForm'].submit();
</script>

Yes, I realize and I did get your example working. I'm now just
experimenting to see what's possible - more of a learning exercise really.
Thanks for your help.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top