Response对象 快速通道(17)

在一些网站中为了用户更好的访问某个网站地址,会用到网站的快速通道,利用dropdownlist控件可以在页面有限的情况下,尽可能的节约空间,从而达到用户访问的目的,这也就是页面的简单跳转,可以利用Redirect方法。其一般格式为“Response.Redirect(“网络地址”);”这里的网络地址可以是绝对的URL(例如:http://www.baidu.com)也可以是相对的地址(例如:default.aspx);

实例:实例部分我们共建立了四个网页,其中有.aspx结尾的是3个(Default.aspx,shenzhoulong.aspx和xuexi.aspx),以.php结尾的一个,以Default.aspx为起始页,其他网页在默认文件夹下,以供dropdownlist索引。在shenzhoulong.aspx和xuexi.aspx中都运用了广告控件Adrotator,创建了xml文件,编辑广告控件,并有学习该控件的相关链接(http://www.cnblogs.com/shenzhoulong/archive/2010/04/22/1717821.html),其中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);
}
}
}

主代码部分就是这些,其余部分请到http://shenzhoulong.blog.51cto.com/1191789/314133下载相关源代码,下面是代码实现的效果图:

默认起始页

起始页

跳转shenzhoulong.aspx

跳转shenzhoulong

跳转xuexi.aspx

跳转xuexi

我们会发现,在点击dropdownlist时并没有出现shenzhoulong.php页面,因为default.aspx后台代码中我们只是读取的以.aspx结尾的对象,所以不会显示出现,以上事件都是经过Button1按钮触发的。继续学习中、、、、、、、

神舟龙

原文地址:https://www.cnblogs.com/shenzhoulong/p/1732795.html