简体   繁体   中英

Dyanmic change button postback url asp.net

is it possible to dynamic changing a button post back url property? For example I got 3 page which is salesListing.aspx , orderlisting.aspx and createDetail.aspx .

The salesListing and orderlisting allow user to navigate to createDetail page. However in the createDetail.aspx I got a back button.

Is it possible the back button can detect which pages cause the postback? And, when user click the back button, the back button will save record and bring it back to the previous page?

For example user click orderlisting then click createDetail page. when he click the back button , the back button save user id and bring user back to the orderlisting .

You can use Link button and if you are using master page then assign relevant Navigation URL according to which page are at. Like you have a link button

<asp:HyperLink ID="HyperLink8" NavigateUrl="Login.aspx"
                            runat="server">Login</asp:HyperLink>

And in code behind you can change its URL.

if (Session["User"] != null)
{
    HyperLink8.Text = "Logout";
    HyperLink8.NavigateUrl = "Logout.aspx";
}
else
{
    HyperLink8.Text = "Login";
    HyperLink8.NavigateUrl = "Login.aspx";
}

I've written a project that will manage all that back navigation for you automatically, http://navigation.codeplex.com/ .

You configure your pages as a list of states. Below each state are the possible transitions to different states. For example, your configuration would be:

<state key="sales" page="~/salesListing.aspx">
    <transition key="next" to="create"/>
</state>
<state key="order" page="~/orderListing.aspx">
    <transition key="next" to="create"/>
</state>
<state key="create" page="~/createDetail.aspx"/>

Then in code, to move from salesListing to createDetail you write:

StateController.Navigate("Next");

And to go back from createDetail to salesListing you write:

StateController.NavigateBack(1);

If you're interested or want any help, let me know.

An option would just be to use the javascript history object.

On your button, put the code OnClientClick="javascript: history.go(-1); return false;"

This will just go to the previous page.

<asp:Button id="btnClick" runat="Server" text="Back" OnClientClick="javascript: history.go(-1); return false;" />

use Request.UrlReferrer.ToString(); and store it on the first page load.

if( !IsPostBack )

{ ViewState["PreviousPageUrl"] = Request.UrlReferrer.ToString(); }

After redirecting to 2nd page and if you want to go back

get the ViewState["PreviousPageUrl"] value and go back using Response.Redirect(ViewState["PreviousPageUrl"].ToString())

Use normal html button and add history.back() function.

<input type="button" Value="Back" onClick="History.back();">

This will work on all the browsers and no server side code/ Postback required.

Happy Coding!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM