日期:2014-05-17 浏览次数:20726 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.App_Code
{
/// <summary>
/// 连接工厂类
/// </summary>
public class Link
{
//构建绝对URL
private static string BuildAbsolute(string relativeUri)
{
//获取当前URL
Uri uri = HttpContext.Current.Request.Url;
//构建绝对路径
string app = HttpContext.Current.Request.ApplicationPath;
if (!app.EndsWith("/"))
{
app += "/";
}
relativeUri = relativeUri.TrimStart('/');
//返回绝对路径
return HttpUtility.UrlPathEncode(string.Format("http://{0}:{1}{2}{3}",uri.Host,uri.Port,app,relativeUri));
}
//生成一个门类url
public static string ToDepartment(string departmentId, string page)
{
if (page=="1")
{
return BuildAbsolute(string.Format("Catalog.aspx?DepartmentId={0}",departmentId));
}
else
{
return BuildAbsolute(string.Format("Catalog.aspx?DepartmentId={0}&Page={1}",departmentId,page));
}
}
//为首页生成一个门类URL
public static string ToDepartment(string departmentId)
{
return ToDepartment(departmentId,"1");
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="List" runat="server" CssClass="DepartmentList" Width="200px">
<HeaderStyle CssClass="DepartmentListHead" />
<HeaderTemplate>
选择一个
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Link.ToDepartment(Eval("DepartmentID").ToString())%>' Text='<%# HttpUtility.HtmlEncode(Eval("Name").ToString()) %>' ToolTip='<%# HttpUtility.HtmlEncode(Eval("Description").ToString()) %>' CssClass='<%# Eval("DepartmentID").ToString()==Request.QueryString["DepartmentID"]?"DepartmentSelected" :"DepartmentUnselected" %>'></asp:HyperLink>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>