zoukankan      html  css  js  c++  java
  • [代码]强制浏览器重定向到另一页

    重点总结
    在ASP.NET中要想实现网页的重定向,可以通过以下4种方式来实现:
    1、通过超链接重定向。
    2、使用跨页面发送技术重定向网页。
    3、通过浏览器编程重定向网页。
    4、通过服务器重定向网页。

    本示例所演示的是如何通过浏览器进行重定向。
    其实,使用浏览器来进行重定向,也分为两种情况:
    一种情况是编写客户端脚本来进行重定向,本示例不包含这方面演示。
    另外情况就是由服务器强制浏览器进行重定向,也就是调用Response.Redirect()方法。这是本示例代码的演示重点。
    需要说明的是,再调用Redirect()方法之前,最好将Response.BufferOutput设置为true,也就是启用内容缓冲。

    示例代码
    在网页上提供了一个下拉列表,其中列出用户可能使用的一些语言。用户可以选择这个列表中的任意一种语言,然后单击【更改语言】按钮,这样就把网页回发给服务器了。
    然后,服务器根据用户选择语言,重定向到合适的网页。具体信息参看代码:

    网页界面设计:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Redirect.aspx.cs" Inherits="Redirect" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>使用浏览器将用户重定向到另一页。Response.Redirect()</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            请选择你所使用的语言:
            <br />
            <asp:DropDownList ID="LanguagesList" runat="server">
                <asp:ListItem Selected="True">China</asp:ListItem>
                <asp:ListItem>Deutsch</asp:ListItem>
                <asp:ListItem>English</asp:ListItem>
                <asp:ListItem>Espa?ol</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="ChangeLanguageButton" runat="server" Text="更改语言" 
                onclick="ChangeLanguageButton_Click" />
        </div>
        </form>
    </body>
    </html>
    

    用来重定向的代码如下,也就是【更改语言】按钮的单击事件处理程序代码:

    protected void ChangeLanguageButton_Click(object sender, EventArgs e)
    {
        this.Response.BufferOutput = false;
        switch (this.LanguagesList.SelectedValue)
        {
            case "China":
                this.Response.Redirect("China.htm");
                break;
            case "Espa?ol":
                this.Response.Redirect("Espa?ol.htm");
                break;
            case "Deutsch":
                this.Response.Redirect("Deutsch.htm");
                break;
            default:
                this.Response.Redirect("English.htm");
                break;
        }
    }
    
  • 相关阅读:
    与众不同 windows phone (50)
    与众不同 windows phone (49)
    重新想象 Windows 8.1 Store Apps (93)
    重新想象 Windows 8.1 Store Apps 系列文章索引
    重新想象 Windows 8.1 Store Apps (92)
    重新想象 Windows 8.1 Store Apps (91)
    重新想象 Windows 8.1 Store Apps (90)
    重新想象 Windows 8.1 Store Apps (89)
    重新想象 Windows 8.1 Store Apps (88)
    重新想象 Windows 8.1 Store Apps (87)
  • 原文地址:https://www.cnblogs.com/GJYSK/p/1872398.html
Copyright © 2011-2022 走看看