简体   繁体   English

ASP.Net C#-重定向到页面

[英]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. 在每个click事件中,我想将用户重定向到特定的(子)页面。 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. 在下面的示例中,我想将用户重定向到browser.aspx,但是,如果用户已经在browser.aspx上,则我不想刷新它。

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. 当您使用browse.aspx时,可以在browse.aspx.cs后面的代码中禁用该按钮。

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: 如果您决定使用javascript在页面之间进行切换,则可以使用location:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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