前台代码:
1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebPage.ascx.cs" 2 Inherits="MyUrlRewriter.WebPage" %> 3 4 <asp:Repeater ID="Repeater1" runat="server" 5 onitemdatabound="Repeater1_ItemDataBound"> 6 <HeaderTemplate> 7 <table> 8 </HeaderTemplate> 9 <ItemTemplate> 10 <asp:Literal ID="Literal4" runat="server"></asp:Literal> 11 </ItemTemplate> 12 <FooterTemplate> 13 </table> 14 </FooterTemplate> 15 </asp:Repeater> 16 第<asp:Literal ID="Literal3" runat="server" Text="1">1</asp:Literal>页 17 共<asp:Literal ID="Literal1" runat="server"></asp:Literal>条数据 18 共<asp:Literal ID="Literal2" runat="server"></asp:Literal>页 19 20 <br/> 21 <asp:LinkButton ID="firstPage" runat="server" onclick="firstPage_Click">首页</asp:LinkButton> 22 <asp:LinkButton ID="upPage" runat="server" onclick="firstPage_Click">上一页</asp:LinkButton> 23 <asp:LinkButton ID="downPage" runat="server" onclick="firstPage_Click">下一页</asp:LinkButton> 24 <asp:LinkButton ID="lastPage" runat="server" onclick="firstPage_Click">末页</asp:LinkButton> 25 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 26 onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 27 </asp:DropDownList> 28 29 <div id="pan1" runat="server"></div>
后台代码:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!IsPostBack) 4 { 5 Literal3.Text = Request.QueryString["index"] ?? "1"; 6 fenyeFuntion(Repeater1, DropDownList1, Literal1, Literal2, "select * from K_U_Special", ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 7 } 8 } 9 10 /// <summary> 11 /// 伪静态分页 12 /// </summary> 13 /// <param name="repeater">Repeater数据绑定控件</param> 14 /// <param name="dropDownList">下拉选择页</param> 15 /// <param name="dataCount">数据总数</param> 16 /// <param name="pageCount">数据页数总数</param> 17 /// <param name="sqlstr">分页sql语句</param> 18 /// <param name="connstr">数据库连接字符串</param> 19 void fenyeFuntion(Repeater repeater,DropDownList dropDownList,Literal dataCount,Literal pageCount, string sqlstr,string connstr) 20 { 21 PagedDataSource page = new PagedDataSource(); 22 DataTable dt = SQLhelp.SqlDataAdapter(connstr, sqlstr, null).Tables[0]; 23 page.DataSource = dt.DefaultView; 24 page.AllowPaging = true; 25 page.PageSize = 5; 26 dataCount.Text = Convert.ToString(page.DataSourceCount); 27 pageCount.Text = Convert.ToString(page.PageCount); 28 29 page.CurrentPageIndex = Convert.ToInt32(Literal3.Text.Trim()) - 1; 30 31 repeater.DataSource = page; 32 repeater.DataBind(); 33 34 if (page.IsFirstPage) 35 { 36 upPage.Enabled = false; 37 firstPage.Enabled = false; 38 } 39 if (page.IsLastPage) 40 { 41 downPage.Enabled = false; 42 lastPage.Enabled = false; 43 } 44 StringBuilder text = new StringBuilder(); 45 for (int i = 0; i < page.PageCount; i++) 46 { 47 if ((i + 1).ToString().Equals(Literal3.Text)) 48 { 49 text.Append("<a style="color:Red;margin-right:15px" href="/web2/" + (i + 1) + "">" + (i + 1) + "</a>"); 50 } 51 else 52 { 53 text.Append("<a style="margin-right:15px" href="/web2/" + (i + 1) + "">" + (i + 1) + "</a>"); 54 } 55 dropDownList.Items.Add("第" + (i + 1) + "页"); 56 } 57 58 pan1.InnerHtml = text.ToString(); 59 dropDownList.SelectedIndex = Convert.ToInt32(Request.QueryString["index"] ?? "1") - 1; 60 61 62 } 63 64 65 66 protected void firstPage_Click(object sender, EventArgs e) 67 { 68 LinkButton link = (LinkButton)sender; 69 if (link.ID == "firstPage") 70 { 71 Response.Redirect("/web2/1"); 72 } 73 else if (link.ID == "upPage") 74 { 75 int index = Convert.ToInt32(Literal3.Text) - 1; 76 Response.Redirect("/web2/" + index + ""); 77 } 78 else if (link.ID == "downPage") 79 { 80 int index = Convert.ToInt32(Literal3.Text) + 1; 81 Response.Redirect("/web2/" + index + ""); 82 } 83 else 84 { 85 Response.Redirect("/web2/" + Literal2.Text + ""); 86 } 87 } 88 89 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 90 { 91 string index = Convert.ToString(DropDownList1.SelectedIndex + 1); 92 Response.Redirect("/web2/" + index + ""); 93 } 94 95 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 96 { 97 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 98 { 99 DataRowView drv = e.Item.DataItem as DataRowView; 100 if (e.Item.FindControl("Literal4") != null) 101 { 102 Literal liter = e.Item.FindControl("Literal4") as Literal; 103 liter.Text += "<tr><td>" + drv["Title"].ToString() + "</td><td>" + drv["AddDate"].ToString() + "</td></tr>"; 104 } 105 } 106 }
重写代码:
1 public void Init(HttpApplication context) 2 { 3 context.BeginRequest += new EventHandler(context_BeginRequest); 4 } 5 6 void context_BeginRequest(object sender, EventArgs e) 7 { 8 9 HttpApplication application = sender as HttpApplication; 10 HttpContext content = application.Context; 11 12 string url = content.Request.Url.LocalPath; 13 if (url.Contains("new")) 14 { 15 string id = url.Substring(url.LastIndexOf("/") + 1); 16 content.RewritePath("/news/new.aspx?id="+id); 17 } 18 else if (url.Contains("product")) 19 { 20 string id = url.Substring(url.LastIndexOf("/") + 1); 21 content.RewritePath("/product/product.aspx?id=" + id); 22 } 23 else if (url.Contains("web2")) 24 { 25 string index = url.Substring(url.LastIndexOf("/") + 1); 26 content.RewritePath("/web2.aspx?index=" + index); 27 } 28 }