ASP.NET通過更改Url進行頁面?zhèn)髦档膶崿F(xiàn)代碼
這里,通過假數(shù)據(jù),手動創(chuàng)建的一個類,然后創(chuàng)建的一個集合,放入下拉框,選好值以后,點確定
會在另一個頁面產(chǎn)生對應(yīng)的id
創(chuàng)建一個類:
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; } } }
一個選擇的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" >查詢</a></p> </form> </body> </html>
選擇的web窗體的后臺代碼
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() { //手動創(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; //默認顯示的值 this.DropDownList1.DataTextField = "DeptName"; this.DropDownList1.DataValueField = "Id"; //保存 this.DropDownList1.DataBind(); } } }
建一個繼承Modules類
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) { //處理請求 //獲取請求url HttpApplication application = sender as HttpApplication; //相對路徑 string url = application.Request.RawUrl; //一個正則,用來匹配是不是相對應(yīng)的頁面 Regex regex = new Regex(@"dept_(\d+).html"); //正則的匹配后的,微軟給鋪好的路,正則匹配后的一個數(shù)組; GroupCollection groupCollection = regex.Match(url).Groups; //這里取得是數(shù)組的第一個值,看看是不是成功匹配了, if (groupCollection[0].Success) { //取到第二個值 var id = groupCollection[1].Value.Trim('_'); //存儲id,等用到的時候直接去第二個頁面去取值 HttpContext.Current.RewritePath("~/DeptDetail.aspx","","deptid="+id); } } } }
建完了類,要進入配置文件進行配置
因為我這里是放在一個文件夾下面了,所以配置文件指定type的時候,要加一個文件夾的路徑
<system.webServer> <modules> <add name="Module" type="WebApplication1.Modules.DeptModule"/> </modules> </system.webServer>
顯示的web窗體
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DeptDetail.aspx.cs" Inherits="WebApplication1.DeptDetail" %> <!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:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form> </body> </html>
顯示的web窗體的后臺代碼
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 DeptDetail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //直接通過request獲取Module存入的id this.TextBox1.Text = $"{Request.QueryString["deptid"]}"; } } } }
效果圖
選擇一個后點擊查詢
地址欄和內(nèi)容都進行了更改
到此這篇關(guān)于ASP.NET通過更改Url進行頁面?zhèn)髦档奈恼戮徒榻B到這了,更多相關(guān)asp.net url 頁面?zhèn)髦祪?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
獲取Repeter的Item和ItemIndex/CommandArgument實現(xiàn)思路與代碼
Repeater控件,放在ItemTemplate內(nèi)的銨鈕OnClick之后,獲取Repeater的Item,ItemIndex,CommandArgument,CommandName以及綁定的字段值附演示動畫感興趣的朋友可以了解下2013-01-01運行page頁面時的事件執(zhí)行順序及頁面的回發(fā)與否深度了解
page頁面時的事件執(zhí)行順序的了解對于一些.net開發(fā)者起到者尤關(guān)重要的作用;頁面的回發(fā)與否會涉及到某些事件執(zhí)行與不執(zhí)行,在本文中會詳細介紹,感興趣的朋友可以了解下2013-01-01數(shù)據(jù)綁定之DataFormatString使用介紹
DataFormatString是很多Asp.Net控件都有的屬性,如GridView等等,下面簡單介紹一下這個屬性,感興趣的朋友不要錯過2013-10-10在ASP.Net?Core應(yīng)用程序中使用Bootstrap4
這篇文章介紹了在ASP.Net?Core應(yīng)用程序中使用Bootstrap4的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01Asp.net利用JQuery AJAX實現(xiàn)無刷新評論思路與代碼
Asp.net利用JQuery AJAX實現(xiàn)無刷新評論,此功能是每一個從事asp.net開發(fā)者的朋友都希望實現(xiàn)的,本文利用閑暇時間整理了一些,有需要的朋友可以參考下2012-12-12win10下ASP.NET Core部署環(huán)境搭建步驟
這篇文章主要以圖文結(jié)合的方式介紹了win10下ASP.NET Core部署環(huán)境搭建步驟,感興趣的小伙伴們可以參考一下2016-07-07ASP.NET的適配器設(shè)計模式(Adapter)應(yīng)用詳解
有關(guān)設(shè)計模式的適配器模式(Adapter)確實不是很好理解理解,接下來將做一個簡單的例子簡要說明下,感興趣的朋友可不要錯過了哈,希望本文可以幫助到你更好的理解適配器設(shè)計模式2013-02-02Asp.NET Core 如何調(diào)用WebService的方法
這篇文章主要介紹了Asp.NET Core 如何調(diào)用WebService的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08