1. 程式人生 > >asp.net-頁面跳轉的方法及例項

asp.net-頁面跳轉的方法及例項

例項如下:

.aspx程式碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_01.aspx.cs" Inherits="Sample_01" %>

<!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></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>頁面轉向</h3>
        <p><span class ="spMemo">通過靜態的超連結轉向:</span>
            <a href ="http://www.51zxw.net" title="我要自學網">我要自學網</a>
        </p>

        <p ><span class="spMemo">動態建立的HyperLink超連結:</span> </p>
            <p class ="pLine"> <span >名稱:</span>         <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>   </p>
            <p class ="pLine"> <span >網址:</span>         <asp:TextBox ID="txt_url" runat="server" Width="300"></asp:TextBox>   </p>
            <p class="pLine">  <asp:Button ID="btn_addlink" runat="server" Text="新增新連結" 
                    onclick="btn_addlink_Click" /> 
                <asp:TextBox ID="txt_links" runat="server" Height="25px" Width="329px" Visible="false" ></asp:TextBox></p>
        <asp:Panel ID="pnl_links" runat="server">
        </asp:Panel>
        
        <p><span class ="spMemo">通過程式語句Redirect跳轉到我要自學網:</span>  
            <asp:Button ID="Button1" runat="server" Text="Response.Redirect跳轉" 
                onclick="Button1_Click" />
        </p>

        <p><span class="spMemo">通過ServerTransfer跳轉到頁面default.aspx</span>
            <asp:Button ID="Button2" runat="server" Text="Server.transfer跳轉" 
                onclick="Button2_Click" />
        </p>

        <p><span class="spMemo">通過帶PostBackUrl屬性的Linkbutton跳轉到頁面default.aspx</span>
            <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Sample_01.aspx" 
                onclick="LinkButton1_Click">我是LinkButton</asp:LinkButton>
        </p>
            
    </div>
    </form>
</body>
</html>

.aspx.cs程式碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sample_01 : System.Web.UI.Page
{


    //自定義函式,新增HyperLink
    protected void AddLink(string nameurl) {
        string[] _link = nameurl.Replace("\u0081", "").Split('\u0080');
        HyperLink hl= new HyperLink();
        hl.Text = _link[0];
        hl.NavigateUrl = _link[1];
        pnl_links.Controls.Add(hl);
    
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        //這段程式,和上一節課最後的練習,構建checkbox的思路相同

        //動態建立超連結,從中轉儲存的TXT中讀取
        if (txt_links.Text != "") { 
            //讀取字串,拆分成陣列,每個成員包括名稱和網址 
            string[] arrS_links = txt_links.Text.Split('\u0081');
            foreach (string s in arrS_links) {
                if (s.Length > 0) {//為什麼要大於0才構建?因為拆分的時候會多一個數組成員為空字串
                    //交給子函式
                    AddLink(s);
                }
            }
        }

        //判斷是否有新值要新增:
        string _name, _url;
        _name = txt_name.Text; _url = txt_url.Text;

        if (_name != "" && _url != "") {
            string _addlinktxt = _name + "\u0080" + _url + "\u0081";
            AddLink(_addlinktxt);
            txt_links.Text += _addlinktxt;

            //清空文字框
            txt_name.Text = txt_url.Text = "";
        }



    }
    protected void btn_addlink_Click(object sender, EventArgs e)
    {


    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://www.51zxw.net");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/default.aspx");
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
    }
}

顯示效果如下: