Purpose of PostBackUrl in <asp:Button>

北城余情 提交于 2021-01-27 22:24:14

问题


What's the purpose of using PostBackUrl?
Let's say I have a button which is in Cart.aspx:

 <asp:Button ID="btnContinue" runat="server" Text="Continue Shopping" PostBackUrl="~/Order.aspx"  CssClass="btn" />

That means that I will be redirectd to Order.aspx, rather than staying on original Cart.aspx. Here are my two questions:

  1. I also have a TextBox in Cart.aspx.
    When I click the button, the value of the TextBox will be posted back to Order.aspx rather than original Cart.aspx. Now I think we can only get this value if it is posted back to Cart.aspx, which contains this TextBox.
    What if I want to retrieve this value on the new page?

  2. If there is no way to retrieve any input on Cart.aspx, why do we need to use PostBackUrl? We could just add:

    Response.Redirect("~/Order.aspx") to the Cart.aspx.cs?


回答1:


If you do not specify the entry PostBackUrl, the button will submit the data back to the same page, in your case it is cart.aspx.

The purpose of PostBackUrl is a across-page-posting of data.

If you specify PostBackUrl="~/Order.aspx", your data will be posted back to your Order.aspx page. In your Order.aspx page, you will be able to get your TextBox (which was in cart.aspx) data using:

Page.PreviousPage.FindControl("TextBox1")

You can learn more at https://msdn.microsoft.com/en-us/library/ms178139.aspx




回答2:


Base on my understanding (I might be wrong but).

  1. Web pages are stateless. The value of your textbox disappears once you leave/ or perform a postback. If you need the values from this page, use cookies, viewstate, sessions or query string to be able to retrieve this value/s.

  2. You don't need PostBackUrl but if you prefer to use it, its up to you. And yes, Response.Redirect("~/Order.aspx") can be use after you do whatever you need to do in CodeBehind. PostBackUrl is will do the same but will not execute codes that you might need before redirecting.



来源:https://stackoverflow.com/questions/44307483/purpose-of-postbackurl-in-aspbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!