0

我制作了一个用户控件,可以在我的页面上生成一个超链接列表。问题是它似乎不想工作。这是我的用户控件代码:

<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="DepartmentsList.ascx.cs"
Inherits="BaloonShop.UserControls.DepartmentsList" %>
<asp:DataList ID="list" runat="server" CssClass="departmentslist" Width="200px">
<HeaderStyle CssClass="departmentslisthead" />
<HeaderTemplate>
    Choose a Department
</HeaderTemplate>
<ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Classes.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>

我的文件结构如下:

文件结构

为什么说找不到Classes文件夹?

如果您需要更多信息,请询问。

编辑

链接类代码:

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

namespace BaloonShop.Classes
{
/// <summary>
/// Link factory class
/// </summary>
public class Link
{
    // Builds an absolute URL
    private static string BuildAbsolute(string relativeUri)
    {
        // get current uri
        Uri uri = HttpContext.Current.Request.Url;
        // build absolute path
        string app = HttpContext.Current.Request.ApplicationPath;
        if (!app.EndsWith("/")) app += "/";
        relativeUri = relativeUri.TrimStart('/');
        // return the absolute path
        return HttpUtility.UrlPathEncode(String.Format("http://{0}:{1}{2}{3}", uri.Host, uri.Port, app, relativeUri));
    }

    // Generate a department 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));
    }

    // Generate a department URL for the first page
    public static string ToDepartment(string departmentId)
    {
        return ToDepartment(departmentId, "1");
    }
}

}

4

3 回答 3

0

public link link= new link();在 DepartmentsList.ascx.cs" 将解决你的问题。使用这个: NavigateUrl='<%# Link.ToDepartment(Eval("DepartmentID").ToString())%>'

于 2012-09-24T10:29:38.197 回答
0

右键单击您的项目(在 Visual Studio 中)并选择添加 ASP.NET 文件夹,然后选择App_Code。接下来将您的类文件夹拖到 App_Code。然后它可能会解决问题

于 2012-09-24T10:12:54.353 回答
0

您的代码只是指Classes. 除非您已经在导入BaloonShop,否则 this 仅指的是global::Classes,但您的命名空间是global::BaloonShop.Classes.

所以:使用BaloonShop.Classes而不是Classes

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# BaloonShop.Classes.Link.ToDepartment(Eval("DepartmentID").ToString())%>'
于 2012-09-24T10:07:54.243 回答