Response.Redirect打開新窗口的兩種方法

一般情況,Response.Redirect的方法是服務器端時行轉向,因此,隊非使用Response.write("<script>window.location='http://dotnet.aspx.cc';</script>)方法外,是不能在新窗口打開所指定的URL地址,但是如果仔細分析一下,如果設置 form元素的rarget屬性,還是有辦法打開新窗口的。

方法一:在服務器端設置 target屬性,這個方法也非常適用於客戶端不支持腳本的情況,代碼如下:

 

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        form1.Target = "_blank";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://dotnet.aspx.cc");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body id="b" runat="server">
<form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="打開新窗口或者新 Tab " />
</form>
</body>
</html>


方法二:採用客戶端設置target屬性。代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "this.form.target='_newName'");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://dotnet.aspx.cc");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body id="b" runat="server">
<form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="打開新窗口或者新 Tab " />
</form>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章