1. 程式人生 > 程式設計 >ASP.NET通過更改Url進行頁面傳值的實現程式碼

ASP.NET通過更改Url進行頁面傳值的實現程式碼

這裡,通過假資料,手動建立的一個類,然後建立的一個集合,放入下拉框,選好值以後,點確定
會在另一個頁面產生對應的id

建立一個類:

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()
  {
   //手動建立資料
   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;
   //一個正則,用來匹配是不是相對應的頁面
   Regex regex = new Regex(@"dept_(\d+).html");
   //正則的匹配後的,微軟給鋪好的路,正則匹配後的一個數組;
   GroupCollection groupCollection = regex.Match(url).Groups;
   //這裡取得是陣列的第一個值,看看是不是成功匹配了,
   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"]}";
   }
  }
 }
}

效果圖

選擇一個後點擊查詢

在這裡插入圖片描述

位址列和內容都進行了更改

在這裡插入圖片描述

到此這篇關於ASP.NET通過更改Url進行頁面傳值的文章就介紹到這了,更多相關asp.net url 頁面傳值內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!