迎接2012之Url重写

一、代码存储过程基于三层架构那篇博文,只需要修改几个页面

二、代码

(1)Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>用户列表</title>
    <script type="text/javascript" src="JQueryUi/js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="JQueryUi/js/jquery-ui-1.8.16.custom.min.js"></script>
    <link type="text/css" rel="Stylesheet" href="JQueryUi/css/ui-lightness/jquery-ui-1.8.16.custom.css" />
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Btn_Add").click(function () {
                $("#hidediv").dialog({
                    modal: true,
                    title: "增加会员",
                    resizable: false,
                    bgiframe: true,
                    open: function () { $("#hidediv").load("Add.aspx") }
                });
            })
            $("#code").css("cursor", "pointer");
            $("#code").click(function () {
                $("#code").attr("src", "VerificationCode.ashx?t=" + function () {
                    var dt = new Date();
                    var dts = dt.getYear().toString() + dt.getMonth().toString() + dt.getDay().toString() + dt.getHours().toString() + dt.getMinutes().toString() + dt.getSeconds().toString() + dt.getMilliseconds().toString();
                    return dts;
                } ());
            });
            PageList(1);

        });
        function PageList(pageno) {
            $.ajax({
                type: "get",
                url: "GetList.ashx?pageno=" + pageno,
                dataType: "json",
                success: function (dt) {
                    var cc = eval(dt);
                    if (cc.RecordCount > 0) {
                        var htm = "<table><tr><th>编号</th><th>用户名</th><th>密码</th><th>性别</th><th>操作</th>";
                        for (var i = 0; i < cc.Data.length; i++) {
                            htm += "<tr id=\"" + cc.Data[i].Id + "\"><td>" + cc.Data[i].Id + "</td><td>" + cc.Data[i].UserName + "</td><td>" + cc.Data[i].PassWord + "</td><td>" + cc.Data[i].Sex + "</td><td><span style=\"cursor:pointer\"onclick=\"Del(" + cc.Data[i].Id + ")\">删除</span> <span style=\"cursor:pointer\"onclick=\"Info(" + cc.Data[i].Id + ")\">查看</span></td></tr>";
                        }
                        htm += "<tr><td colspan=\"5\" align=\"center\">";
                        if (pageno > 1) {
                            htm += "<span onclick=\"PageList(" + (1) + ")\">第一页</span>";
                            htm += "<span onclick=\"PageList(" + (pageno - 1) + ")\">上一页</span>";
                        }
                        if (pageno < cc.PageCount) {
                            htm += "<span onclick=\"PageList(" + (pageno + 1) + ")\">下一页</span>";
                            htm += "<span onclick=\"PageList(" + (cc.PageCount) + ")\">最后页</span>";
                        }
                        htm += "共" + cc.PageCount + "页码共" + cc.RecordCount + "条记录 当前第" + pageno + "页</td></tr>";
                        $("#content table").html(htm);
                    }
                },
                beforeSend: function (dt) {
                    alert("正在获取");
                },
                Error: function (dt) {
                    alert(dt);
                }
            });
        }
        function Del(id) {
            $.ajax({
                type: "post",
                url: "Delete.ashx",
                data: "id=" + id + "&time=" + function () {
                    var dt = new Date();
                    var dts = dt.getYear().toString() + dt.getMonth().toString() + dt.getDay().toString() + dt.getHours().toString() + dt.getMinutes().toString() + dt.getSeconds().toString() + dt.getMilliseconds().toString();
                    return dts;
                } (),
                success: function (dt) {
                    alert(dt);
                    $("#" + id).remove();
                },
                Error: function (dt) {
                    alert("出错啦!");
                }
            });
            return false;
        }
        function Info(id) {
            $("#hidediv").dialog({
                modal: true,
                title: "查看会员",
                resizable: false,
                bgiframe: true,
                open: function () { $("#hidediv").load("Select" + id + ".ashx") }
            });
        }
    </script>
    <style type="text/css">
        *
        {
            margin:0px;
            padding:0px;
        }
        table:
        {
            border-collapse:collapse;
        }
        table tr th,td
        {
            border-width:1px;
            border-style:solid;
            border-color:Blue;
        }
    </style>
</head>

<body>
    <div>
        <input id="Btn_Add" type="button" value="增加用户" />
    </div>
    <div id="content">
        <table>
            <tr>
                <th>
                    编号
                </th>
                <th>
                    用户名
                </th>
                <th>
                    密码
                </th>
                <th>
                    性别
                </th>
                <th>
                    操作
                </th>
            </tr>
            <tr>
            <td colspan="5">
            共1页 总共0条记录 当前第1页
            </td>
            </tr>
        </table>
    </div>
    <div id="hidediv">
    </div>
</body>
</html>

(2)Select.ashx

<%@ WebHandler Language="C#" Class="Select" %>

using System;
using System.Web;

public class Select : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string str = string.Empty;
        context.Response.ContentType = "text/plain";
        int id = int.Parse(context.Request.QueryString["id"].ToString());
        ThreeLevelBLL.Users user = new ThreeLevelBLL.Users();
        ThreeLevelMODEL.Users model = user.GetUserId(id);
        if (model != null)
        {
            str = "用户名:" + model.UserName + "<br/>密码:" + model.PassWord + "<br/>性别:" + (model.Sex == true ? "男" : "女");
        }
        context.Response.Write(str);
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

(3)UrlWrite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///UrlWrite 的摘要说明
/// </summary>
public class UrlWrite:IHttpModule
{
	public UrlWrite()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        HttpContext context = application.Context;
        string strurl = context.Request.RawUrl.Split('/')[context.Request.RawUrl.Split('/').Length - 1];
        if (strurl.Substring(0, 6) == "Select" && strurl.Split('.')[strurl.Split('.').Length - 1].ToLower().Trim() == "ashx")
        {
            string newurl = "/ThreeLevelWeb/Select.ashx?id=" + strurl.Split('.')[0].Substring(6, 1);
            context.RewritePath(newurl);
        }
    }
}

(4)Web.Config

<?xml version="1.0"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
	<connectionStrings>
		<add name="ConStr" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|ThreeLevelData.mdf;User Instance=true"/>
	</connectionStrings>
	<system.web>
		<compilation debug="true" targetFramework="4.0"/>
    <httpModules>
      <add name="UrlWrite" type="UrlWrite"/>
    </httpModules>
	</system.web>
</configuration>

三、效果图

四、可能会有错误,尚未仔细测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值