在一些网站中为了用户更好的访问某个网站地址,会用到网站的快速通道,利用dropdownlist控
件可以在页面有限的情况下,尽可能的节约空间,从而达到用户访问的目的,这也就是页面的简单跳转,可以利用Redirect方法。其一般格式为
“Response.Redirect(“网络地址”);”这里的网络地址可以是绝对的URL(例如:http://www.baidu.com)也可以
是相对的地址(例如:default.aspx);
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="快速通道._Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server" target="_parent">
<div>
快速通道示例<hr style="color:Red"/>
<asp:DropDownList ID="DropDownList1" Height="19px" Width="444px" AutoPostBack="true" CausesValidation="false" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="跳转" onclick="Button1_Click1" />
</div>
</form>
</body>
</html>
Default.aspx的后台代码为
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace 快速通道
{
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] files=Directory.GetFiles(Server.MapPath("./"),"*.aspx");//读取以.aspx结尾的对象
for (int i = 0; i < files.Length; i++)
{//通过循环把以.aspx结尾的对象写入DropDownList控件中
DropDownList1.Items.Add(files[i].Substring(files[i].LastIndexOf("\\") + 1));
}
}
}
protected void Button1_Click1(object sender, EventArgs e)
{//提交按钮触发的事件,实现跳转
Response.Redirect(DropDownList1.SelectedItem.Text);
}
}
}
默认起始页
跳转shenzhoulong.aspx
跳转xuexi.aspx
我们会发现,在点击dropdownlist时并没有出现shenzhoulong.php页面,因为default.aspx后台代码中我们只是
读取的以.aspx结尾的对象,所以不会显示出现,以上事件都是经过Button1按钮触发的。继续学习中、、、、、、、