在aspx页怎么引用自己写的类?进来帮看看啊
我在app_code文件夹中写了一个类,在aspx页怎么无法调用,提示:编译器错误消息: CS0103: 当前上下文中不存在名称“Link”,类代码如下:
- C# code
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"); } }}
aspx页代码如下:
- HTML code
<%@ 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>
[解决办法]
<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>
------------------------------------
这块可以调用 本页面的.cs 方法,在.cs方法内再去调用Link里面的方法
[解决办法]
是不是少了这个
using WebApplication1.App_Code;
[解决办法]
<%@ Import Namespace="WebApplication1.App_Code" %>
[解决办法]
导入命名空间
比如:
- C# code
<%@Import Namespace="System.Data"%>
[解决办法]
但是咋们一般不这么写吧 难道不能直接在后台里面直接处理掉?
[解决办法]
重新编译一次 或者把命名空间改个名字
[解决办法]
[解决办法]
可以尝试查看app_code文件夹的支持项目类型列表,就我所知WEb应用程序不支持(很多限制)。一般在项目上无法从ASPNET文件夹中找到该项,就不要瞎折腾自己去加了。
类库你随便放个地方都可以,反正根据命名空间引用...