CurrentExecutionFilePath的解释上:获取当前请求的虚拟路径。
见代码:
后台
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 databind();
6 this.HyperLink1.Visible = true;
7 this.HyperLink4.Visible = true;
8 }
9 }
10 public void databind()
11 {
12 SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True");
13 SqlCommand cmd = new SqlCommand("select * from Customers", con);
14 SqlDataAdapter da = new SqlDataAdapter(cmd);
15 DataSet ds = new DataSet();
16 da.Fill(ds);
17 PagedDataSource ps = new PagedDataSource();
18 ps.DataSource = ds.Tables[0].DefaultView;
19 ps.AllowPaging = true;
20 int pageIndex;
21 if (Request.QueryString["page"] == null)
22 {
23 pageIndex = 0;
24 }
25 else
26 {
27 pageIndex = Convert.ToInt32(Request.QueryString["page"]);
28 }
29 ps.CurrentPageIndex = pageIndex;
30 this.Label1.Text =Convert.ToString( pageIndex + 1);
31 this.Label2.Text = ps.PageCount.ToString();
32 this.HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?page=0";
33 if (pageIndex <= 0)
34 {
35 this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?page=0";
36 }
37 else
38 {
39 this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(pageIndex - 1);
40 }
41 this.HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(Convert.ToInt32(this.Label2.Text) - 1);
42 if ((pageIndex+1) >= Convert.ToInt32(this.Label2.Text))
43
44 {
45 this.HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(Convert.ToInt32(this.Label2.Text) - 1);
46 }
47 else
48 {
49 this.HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(pageIndex + 1);
50 }
51 this.GridView1.DataSource = ps;
52 this.GridView1.DataBind();
53 }
前台:
前台
1 <div>
2 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
3 <Columns>
4 <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
5 <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
6 <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
7 <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
8 </Columns>
9 </asp:GridView>
10 <asp:HyperLink ID="HyperLink1" runat="server">首页</asp:HyperLink>
11 <asp:HyperLink ID="HyperLink2" runat="server">上一页</asp:HyperLink>
12 <asp:HyperLink ID="HyperLink3" runat="server">下一页</asp:HyperLink>
13 <asp:HyperLink ID="HyperLink4" runat="server">尾页</asp:HyperLink>
14 当前页为:<asp:Label ID="Label1" runat="server" Text="1"></asp:Label>
15 总共<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>页
16 </div>
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏