问题
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.
回答1:
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
回答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.
回答3:
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);
回答4:
I've come across this solution
<asp:LinkButton ID="CartOrderButton" runat="server" PostBackUrl="order.aspx" onclick="CartOrderButton_Click">Order Me</asp:LinkButton>
来源:https://stackoverflow.com/questions/6535473/action-postbackurl-programmatically-using-c-sharp