简体   繁体   中英

ASP.Net C# - Redirect to a page

I've got a 'menu' within a Master page which includes a number of image buttons with click events.

Within each click event, I want to redirect the user to a specific (Child) page. However, if the user is already on the correct (Child) page, I don't want to refresh the page and lose the entered data.

In the example below, I want to redirect the user to browse.aspx however, if the user is already on browse.aspx, I don't want to refresh it.

Is there a better way to do this than the following?

protected void ibtnBrowse_Click(object sender, ImageClickEventArgs e)
{
    if (!Request.Url.Segments[1].ToString().Equals("browse.aspx"))
    {
        Response.Redirect("~/browse.aspx");
    }
}

How about disabling the Image button on the page?

eg

When you are on browse.aspx, in code behind browse.aspx.cs you can disable the button.

ImageButton iBtn = (ImageButton)Page.Master.FindControl("ibtnBrowse");
iBtn.Enabled = false;

By having server side click events the page will always "refresh" when clicking on these buttons. However, if you are simply looking to avoid doing an unnecessary redirect in your code you can use:

HttpContext.Current.Request.Url.AbsoluteUri

or

HttpContext.Current.Request.Url.AbsolutePath

If you decide to use javascript to switch between pages you can use location:

location.replace("http://www.w3schools.com");
location.assign("http://www.w3schools.com");

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