欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-17 15:15:07
这里,通过假数据,手动创建的一个类,然后创建的一个集合,放入下拉框,选好值以后,点确定会在另一个页面产生对应的id创建一个类:using system;using system.collections...

这里,通过假数据,手动创建的一个类,然后创建的一个集合,放入下拉框,选好值以后,点确定
会在另一个页面产生对应的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的时候,要加一个文件夹的路径

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

 <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进行页面传值的实现代码

到此这篇关于asp.net通过更改url进行页面传值的文章就介绍到这了,更多相关asp.net url 页面传值内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!