当前位置: 首页 > 编程笔记 >

ASP.NET通过更改Url进行页面传值的实现代码

赫连永怡
2023-03-14
本文向大家介绍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 页面传值内容请搜索小牛知识库以前的文章或继续浏览下面的相关文章希望大家以后多多支持小牛知识库!

 类似资料:
  • 本文向大家介绍页面间固定参数,通过cookie传值的实现方法,包括了页面间固定参数,通过cookie传值的实现方法的使用技巧和注意事项,需要的朋友参考一下 最后在做的页面,比如用户数据(用户头像,名称,年龄)这些信息,因为大部分页面都要用,之前是通过url地址传,另一页面接收。考虑到这样做会让url过长,因此,尝试使用cookie,把固定的值保存在cookie,其它页面拿出来就可以使用。 在此之前

  • 问题内容: 通过AJAX从标准HTTP页面调用HTTPS页面(例如信用卡授权服务,例如WorldPay)是否会有问题? 我无法想象为什么会有问题,响应将是HTML页面,然后我可以将其嵌入结果窗格或类似的页面? 问题答案: 是的,这将是跨域发布,并且将被浏览器阻止。

  • 本文向大家介绍AngularJS通过$location获取及改变当前页面的URL,包括了AngularJS通过$location获取及改变当前页面的URL的使用技巧和注意事项,需要的朋友参考一下 注意 本文中获取与修改的URL以 ‘http://172.16.0.88:8100/#/homePage?id=10&a=100' 这个路径为例: 一. 获取url的相关方法(不修改URL): 1.获取当

  • 问题内容: 我正在尝试使我的网站页面无缝加载。如果单击下面某些链接上的页面,您将看到我在说什么。 http://www.ultranoir.com/ http://www.itsmassive.com/ 当您单击链接时,它将加载信息,并且/#!/添加到URL。如何添加此功能,以便页面以相同的方式加载?哪里有教程? 问题答案: 这称为事件。您可以在更改后的值而无需重新加载页面,然后可以使用AJAX加

  • 本文向大家介绍Angular 通过注入 $location 获取与修改当前页面URL的实例,包括了Angular 通过注入 $location 获取与修改当前页面URL的实例的使用技巧和注意事项,需要的朋友参考一下 以下获取与修改的 URL 以 ( http://172.16.0.88:8100/#/homePage?id=10&a=100 ) 为例 【一】获取 (不修改URL) 【二】修改 (改

  • 我在Vaadin7做一个项目。因为我需要改变一个页面的主题。 但在Vaadin 7里,我找不到这样的。 我知道会有办法做到的。 以及当我更改主题时如何在UI上应用更改?