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">查询</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进行页面传值的更多相关文章
- 四、angularjs 如何在页面没有登录的情况下阻止用户通过更改url进入页面--$stateChangeStart
有时候用户没有登录或者在某些情况下你是不希望用户进入页面,但是angular的路由机制可以让用户直接通过更改Url进入页面,如何处理这一问题呢? ——监控路由转换机制 $stateChangeStar ...
- ASP.NET MVC 5 Web编程5 -- 页面传值的方式
本篇文章将讲述MVC的页面传值方式,具体包括:后端向前端传值(Controller向View传值):前端向后端传值(View向Controller传值):Action与Action之间的传值. 回顾 ...
- ASP.NET页面传值不使用QueryString
ASP.NET页面传值不使用QueryString Asp.net中的页面传值方法: 1 Url传值 特点:主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在浏览器的地址 ...
- c#ASP.NET中页面传值共有这么几种方式
一.目前在ASP.NET中页面传值共有这么几种方式: 1.Response.Redirect("http://www.hao123.com",false); 目标页面和原页面可以在 ...
- ASP.NET页面传值的几种方式
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值. 存储对象传值.ajax.类.model.表单等!下面欧柏泰克和大家一起来看看asp.net页面传值方式一般有哪些?常用的较简单 ...
- ASP.Net页面传值比较
ASP.Net页面传值比较 作为一个ASP.Net程序员,尤其是搞B/S开发的,对于不同页面之间变量值的传递用的非常广泛,而掌握不同方式之间的区别和特点也就很有必要.本文将针对这一知识点做一个简单 ...
- ASP.NET之页面传值
一.目前在ASP.NET中页面传值共有这么几种方式: 1.使用QueryString变量QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全 ...
- ASP.Net中页面传值的几种方式
开篇概述 对于任何一个初学者来说,页面之间传值可谓是必经之路,却又是他们的难点.其实,对大部分高手来说,未必不是难点. 回想2016年面试的将近300人中,有实习生,有应届毕业生,有1-3年经验的,有 ...
- ASP.NET页面传值与跳转
asp.net页面传值的五种方法:QueryString,Session,Application,Request.Cookies,Server.Transfer 其中Cookie和Server.Tra ...
随机推荐
- Mybatis学习笔记汇总(包括源码和jar包)
博客整理 Mybatis学习笔记(一)--对原生jdbc中问题的总结 Mybatis学习笔记(二)--Mybatis框架 Mybatis学习笔记(三)--入门程序 MyBatis学习笔记(四)--入门 ...
- requestAnimationFrame/cancelAnimationFrame——性能更好的js动画实现方式
用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css3动画出来后,我们又可以使用css3来实现动画了,而且性能和流畅度也得到了很大的提升.但是css3动画还是 ...
- Python脚本:实现excel表格导入到数据库,支持mysql,postgresql,MongoDB
import xlrd,re from datetime import datetime from xlrd import xldate_as_tuple # 判断上传表格是否与模板要求一致 def ...
- 2020网鼎杯 白虎组reverse:hero
主函数,当bossexist的值不为0时,while循环dround()函数,循环结束输出flag outflag()函数的flag值由6段数据拼凑而成 while循环的dround()函数有三个选择 ...
- Azure B2C登录,react-web端实现,自定义登录页面ui
import React, { Component } from 'react'; import Particles from 'react-particles-js'; import { Form, ...
- 微信小程序canvas canvasGetImageData方法真机获得数据显示到image为空白
方法 wx.canvasGetImageData的参数 width,height 只能是整数
- 分布式文件存储库MinIO可还行?
在传统的单体应用架构中,一个应用程序对应一台服务器,提供单进程服务. 但是随着业务的升级,技术的更新迭代,分布式.集群架构.微服务等现已俨然成为主流. 几乎所有的项目都会与文件挂钩,例如OA系统的报表 ...
- HTTP及Web核心基础
1. HTTP服务重要基础 1.1 用户访问网站基本流程 (1)客户端从浏览器输入"www.baidu.com"网站地址,回车后,系统首先会查找系统本地的DNS缓存及hosts文件 ...
- 【雕爷学编程】Arduino动手做(45)---红外避障传感器
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- react-debug
最近练习react的时候遇到一些问题: 在redux模式下,同一个api依据参数获取不同data的时候,返回的data相同 原因:多次调用该接口时,action的type相同,导致对应于该接口的每个r ...