這篇文章主要講解了“ASP.NET如何通過(guò)更改Url實(shí)現(xiàn)頁(yè)面?zhèn)髦怠保闹械闹v解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“ASP.NET如何通過(guò)更改Url實(shí)現(xiàn)頁(yè)面?zhèn)髦怠卑桑?/p>

這里,通過(guò)假數(shù)據(jù),手動(dòng)創(chuàng)建的一個(gè)類(lèi),然后創(chuàng)建的一個(gè)集合,放入下拉框,選好值以后,點(diǎn)確定
會(huì)在另一個(gè)頁(yè)面產(chǎn)生對(duì)應(yīng)的id
創(chuàng)建一個(gè)類(lèi):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Dept
{
public int Id { get; set; }
public string DeptName { get; set; }
}
}一個(gè)選擇的web窗體
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dept.aspx.cs" Inherits="WebApplication1.Dept1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> </asp:DropDownList> </div> <p>><a href="dept_<%=DropDownList1.SelectedValue %>.html" rel="external nofollow" >查詢(xún)</a></p> </form> </body> </html>
選擇的web窗體的后臺(tái)代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Dept1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDeptData();
}
}
private void LoadDeptData()
{
//手動(dòng)創(chuàng)建數(shù)據(jù)
List<Dept> depts = new List<Dept>
{
new Dept{Id=1,DeptName="小明"},
new Dept{Id=2,DeptName="小王"},
new Dept{Id=3,DeptName="小李"}
};
this.DropDownList1.DataSource = depts;
//默認(rèn)顯示的值
this.DropDownList1.DataTextField = "DeptName";
this.DropDownList1.DataValueField = "Id";
//保存
this.DropDownList1.DataBind();
}
}
}建一個(gè)繼承Modules類(lèi)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace WebApplication1.Modules
{
public class DeptModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
//處理請(qǐng)求
//獲取請(qǐng)求url
HttpApplication application = sender as HttpApplication;
//相對(duì)路徑
string url = application.Request.RawUrl;
//一個(gè)正則,用來(lái)匹配是不是相對(duì)應(yīng)的頁(yè)面
Regex regex = new Regex(@"dept_(\d+).html");
//正則的匹配后的,微軟給鋪好的路,正則匹配后的一個(gè)數(shù)組;
GroupCollection groupCollection = regex.Match(url).Groups;
//這里取得是數(shù)組的第一個(gè)值,看看是不是成功匹配了,
if (groupCollection[0].Success)
{
//取到第二個(gè)值
var id = groupCollection[1].Value.Trim('_');
//存儲(chǔ)id,等用到的時(shí)候直接去第二個(gè)頁(yè)面去取值
HttpContext.Current.RewritePath("~/DeptDetail.aspx","","deptid="+id);
}
}
}
}建完了類(lèi),要進(jìn)入配置文件進(jìn)行配置
因?yàn)槲疫@里是放在一個(gè)文件夾下面了,所以配置文件指定type的時(shí)候,要加一個(gè)文件夾的路徑
ASP.NET如何通過(guò)更改Url實(shí)現(xiàn)頁(yè)面?zhèn)髦?創(chuàng)新互聯(lián)
URL鏈接:http://www.chinadenli.net/article36/iidpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、定制開(kāi)發(fā)、軟件開(kāi)發(fā)、網(wǎng)站制作、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容