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 页面传值内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • asp.net 页面传值的几个方法

    在这篇文章里,azamsharp 将为我们介绍一些ASP.NET页面传值的方式.本文所举的例子非常简单,仅仅包含了一个文本框和几个按钮,当按钮点击时文本框中的字符串将会以不同的方式传递到另外的页面去. 1. Response.Redirect (或称 Query String 方式.URL方式) 复制代码 代码如下: Response.Redirect("WebForm5.aspx"); 首先让我们看看 Response.Redirect 方法,这应该是最简单的了,当我们点击Respo

  • ASP.NET网站伪静态下使用中文URL的方法

    首先解释一下,什么是中文URL呢?它并不是我们常见的把汉字编码为 %CF%EC 这种形式,而是在URL中直接使用汉字 这种形式目前还不是很多见.因为不同的浏览器处理起来可能会有所不同,不过据我测试,IE8和Firefox是完全支持的. 它的好处是可以使用链接地址看起来非常直观易懂!搜索引擎也支持. 先讲一下我的ASP.NET网站的伪静态,我的文件都是以.htm结尾的,实际上是动态的ASP.方法是:在后台将htm映射为aspx文件. 伪静态的规则,是在web.config文件中定义的. 形式一:

  • Asp.net中Request.Url的各个属性对应的意义介绍

    1.简单的环境搭建 在本地IIS上配置了一个网站:主机名为wjnhome.com,端口88,然后建了一个虚拟目录指向同一站点,虚拟目录名称为virtual,配置host为127.0.0.1 wjnhome.com 所以地址就为:http://jb51.net:88/virtual/urldemo.aspx?id=2#top 2.编写简单的代码 复制代码 代码如下: //虚拟目录的路径 Response.Write("<strong>Request.ApplicationPath:&l

  • ASP.NET实现页面传值的几种方法小结

    这三种方法是:QueryString,Session和Server.Transfer. 通过URL链接地址传递  send.aspx:  复制代码 代码如下: protected void Button1_Click(object sender, EventArgs e)    {        Request.Redirect("Default2.aspx?username=honge");    } receive.aspx: 复制代码 代码如下: string username

  • asp.net获取当前网址url的各种属性(文件名、参数、域名 等)的代码

    设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb51.net"是域名 "aaa"是站点名 "bbb.aspx"是页面名(文件名) "id=5&name=kelli"是参数 [1]获取 完整url (协议名+域名+站点名+文件名+参数) 复制代码 代码如下: string url=R

  • asp.net页面传值测试实例代码(前后台)

    WebForm_1.aspx内容如下: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_1.aspx.cs" Inherits="页面传值.WebForm_1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu

  • asp.net C#检查URL是否有效的方法

    我们有时候需要对用户输入的网站(URL)进行有效性检查, 复制代码 代码如下: function CheckUrl(str) {    var RegUrl = new RegExp();    RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&?/.=]+$");    if (!RegUrl.test(str)) {        return false;    }    return true;} 不止

  • 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

  • asp下利用XMLHTTP 从其他页面获取数据的代码

    利用XMLHTTP 从其他页面获取数据 我们在编写ASP代码的时候,大家都知道可以通过post或者get获得form表单的数据,那么我们如何直接获得其他页面上的数据呢?这就要借助xmlhttp协议了.xmlhttp是xmldom技术的一部分. 下面的代码就是一个很简单的例子,我们利用xmlhttp技术,把http://www.xxxx.com/站点首页的代码以xml的形式完全获取,并且在页面中输出. <% Dim objXMLHTTP, xml Set xml = Server.CreateOb

  • asp与php中定时生成页面的思路与代码

    PHP版本的的定时生成页面的: <?php $file = dirname(__FILE__).'/index.html'; $timex=time()-filemtime($file); //间隔时间,单位秒 if($timex>7200){ //间隔大于2小时,重新生成 echo "<script language=javascript src='crhtml.php'></script>"; } ?> ASP版本的的定时生成页面的: &l

  • ASP.NET MVC 从IHttp到页面输出的实例代码

    复制代码 代码如下: MVCHandler : IHttpHandlervoid IHttpHandler.ProcessRequest(HttpContext httpContext){    this.ProcessRequest(httpContext);} protected virtual void ProcessRequest(HttpContext httpContext){    HttpContextBase base2 = new HttpContextWrapper(htt

  • layui type2 通过url给iframe子页面传值的例子

    A页面 调用layui.layer.open layui.use(["layer"], function () { layui.layer.open({ type: 2, title: "管理角色拥有的部门", btn: ["确定修改", "关闭"], content: '@Url.Content("~/Role/UserRoleView?rid=")'+a[0].ID + '&uid=' + a[

  • 在ASP.NET中重写URL的代码

    经常有人请我指导应该如何动态地"重写"URL,以在他们的ASP.NETweb应用中发布比较干净的URL端点.这个博客帖子概述了几个方法,你可以用来在ASP.NET中干净地映射或重写URL,以及按照你自己的需求组织你的URL的结构. 为什么URL映射和重写很重要? 下面是开发人员想要对URL有更大的灵活性的最常见的场景: 1) 处理这样的情形:你要更改你的web应用中网页的结构,但你同时也要确保在你移动网页后,那些被人收藏的老URL不会成为死链接.重写URL允许你透明地将请求转交到新的网

  • ASP.NET Core中的Razor页面介绍

    目录 简介 Why? 创建Razor页面应用程序 ASP.NETCoreRazor页面-核心功能 模型绑定 Handlers TagHelpersandHTMLHelpers 路由 总结 简介 随着ASP.NET Core 2 即将来临,最热门的新事物是Razor页面. Razor页面是ASP.NET Core的一个新功能,可以使基于页面的编程方式更容易,更高效. 大众的初步印象是对于那些只专注于页面的小型应用来说,Razor页面更容易.更快地取代MVC.然而,事实证明,它可能比这更强大.使用A

随机推荐