3

Can you get the action of PostBackUrl in a page's code behind?

I want to call an onclick function that does some database updates and then posts back to a different URL with values as hidden fields.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ComfortablyNumb
  • 1,448
  • 10
  • 37
  • 64

4 Answers4

3

I would use server.Transfer However, hidden fields are extemely unsecure. So in it's place (and since you are already using server.transfer) why not use Context.Items? You can transfer whole objects through it. For instance: This is on the originating page -

Context.Items.Add("Contact", contactID)
        Server.Transfer("~/ViewContact.aspx")

On the resulting page you do -

Dim contactID = Context.Items.Item("Contact").ToString()

You have to be extremely careful when using server.transfer as the transfer happens in the server and never notifies the browser that the page is different than the one it requested. This can get you into trouble when you are trying to do postbacks if you are not expecting it.

I actually just wrote a blog on this very thing...

Server.Transfer example

cgcarter1
  • 327
  • 1
  • 8
  • This is a great idea but the url will still be the same (?) - I would really like to see it 'move on' to the next page. – ComfortablyNumb Jun 30 '11 at 15:18
  • 1
    Reading your original text, You really aren't wanting to do a postback, you are just posting to a new page. So essentially, user fills out form, you perform database stuff... Then (with that data) you post to a new page. If you don't care, I would just use the Routing function that's been available since 3.5 SP1. Even if it is secure, you can always encrypt the data. Be wary of using ViewState, it has been hacked. – cgcarter1 Aug 04 '11 at 19:38
  • If you are just posting to a new page.. Just use a Response.Redirect and then use Page History. You can then get a handle on the fields from the prior page. – cgcarter1 Aug 09 '11 at 15:14
2

You create a simple white page with all your input that you like to post back, inside a form, and with javascript you make post back.

The problem is when the user did not have javascript enable, for that case, in this white simple page you have include a message that say to the user that this page automatically redirect to the other page in few seconds, or click here to redirect now.

This simple white page, is just a overwrite of the normal page after the click.

You can also read this post that is a similar trick to post form data to another URL with source code and example.

and there is also many other way to pass values between asp.net web pages.

This is usually case when you have a payment gateway provider, and you won from your page to transfer to that gateway - paypal for example. One other solution is also, to have a page with the summary of what you like to send to the next page, and in that page you have include all you data, and a summary and a normal post back to the gateway.

I need to run a function (in code behind) when a button is clicked. Once that function is finished I then want the code behind to post to a different page with hiddenfield values

When your function is finished you can not move with post back to one other page. What you can do is to render to your page a simple form with your data as hidden input that you need to send to the other page, and make an automatic post this new form that you have render.

Example

protected void Button1_Click(object sender, EventArgs e)
{
   // ...you calculations here...

    StringBuilder sbRenderOnMe = new StringBuilder();

    // the form and the data
    sbRenderOnMe.AppendFormat(
    "<html><body><form action=\"http://www.google.com\" method=\"post\" name=\"form1\">"
         + "<input value=\"{0}\" id=\"lst-Send1\" name=\"Send1\" type=\"hidden\" />"
         + "<input type=\"submit\" name=\"Button1\" value=\"press me if not automatically redirect\" id=\"Button1\" />"
     + "</form>"
     , 1100
    );

    // the auto submit
    sbRenderOnMe.AppendFormat("<script>document.form1.submit();</script>");       
    sbRenderOnMe.AppendFormat("</body></html>");

    Response.Write(sbRenderOnMe.ToString());
    Response.End();
}

The only minus that have a small flickering and not work automatically with out javascript, need and say to the user to press the button.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks for that, but maybe I've not made myself clear. Regardless of Javascript, I need to run a function (in code behind) when a button is clicked. Once that function is finished I then want the code behind to post to a different page with hiddenfield values. (like PostBackUrl) – ComfortablyNumb Jun 30 '11 at 14:03
  • Basically, I'm trying to submit the cart contents but run my recalculate cart function before the page moves on to the order page. I'm rather new to C# and to be honest I didn't understand the examples offered. – ComfortablyNumb Jun 30 '11 at 14:19
  • @ComfortablyNumb exactly what I am show you here. Just read it again to understand the way I propose (and I use) – Aristos Jun 30 '11 at 14:34
  • @ComfortablyNumb I make some code example, did you see it ? I have test it and work, get the google error page :) – Aristos Jul 01 '11 at 10:48
0

After you are done with the original code-behind event ,you can use Server.Transfer and preserve form and query string variables.

Server.Transfer("OtherPage.aspx", true);
Yiğit Yener
  • 5,796
  • 1
  • 23
  • 26
  • The Server.Transfer is not send the new post back values that @ComfortablyNumb ask for. Ask for diferent url with new post data. Both of this fails with Server.Transfer – Aristos Jun 30 '11 at 13:42
  • Request.Form collection will keep the values from the original post when you use "Server.Transfer". You can check this by setting up a simple test project. – Yiğit Yener Jun 30 '11 at 13:52
  • Yes have the original post, but here need to send a different post ! – Aristos Jun 30 '11 at 14:35
0

I've come across this solution

<asp:LinkButton ID="CartOrderButton" runat="server" PostBackUrl="order.aspx" onclick="CartOrderButton_Click">Order Me</asp:LinkButton>
ComfortablyNumb
  • 1,448
  • 10
  • 37
  • 64