2012-04-25 24 views
0

因此,这是我的困境。我正在编写一个定制的,一次性的内容管理系统,而且我不能让这个方法正常工作。我想要做的是在单独的文件夹中创建一个值得使用的方法,并根据需要将它们打电话给我要调用的任何Web表单。无法从Web表单中的App_Code文件夹调用公共列表

我创建了一个WebApp,并在应用程序中创建了一个名为App_Code的文件夹。在App_Code内部,有一个名为“TestimonialService”的公共类。那就是:

那么实际aspx.cs页面上我打电话说,像这样的功能:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using BlueTreeSecurity.App_Code; 
using BlueTreeSecurity.App_Code.Data; 
using BlueTreeSecurity.App_Code.Testimonials; 

namespace BlueTreeSecurity 
{ 
    public partial class Testimonials : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      this.Page.Title = "Testimonials | ..."; 

      Bind_Data(); 
    } 

    protected void Bind_Data() 
    { 
     /** when i try to use intellisense here it's not recognized. **/ 

     var testimonials = TestimonialService.GetAllTestimonials(); 

     rptTestimonials.DataSource = testimonials; 
     rptTestimonials.DataBind(); 
    } 
} 

}

确切的错误吐回是这样的:

Error 1 
An object reference is required for the non-static field, method, or property 
'BlueTreeSecurity.App_Code.Testimonials.TestimonialService.GetAllTestimonials()' 

任何东西将不胜感激,伙计们。我在这里把头发拉出来。

这里的项目结构

- Blue Tree Security (main project) 
    - App_Code 
     + Data 
     + Testimonials 
     + TestimonialService.cs 
    Rest of the .aspx, .aspx.cs, and .ascx files. 
+0

您试图在'TestimonialService'类中使用'GetAllTestimonials'方法作为静态方法。您需要首先实例化TestimonialService类。 – 2012-04-25 11:19:00

回答

2

如果我不能完全曲解这里,你所得到的错误信息,告诉你是什么问题。使GetAllTestimonials()静态或实例化一个TestimonialService实例。

protected void Bind_Data() 
{ 
    var testimonialService = new TestimonialService(yourContextObect); 

    var testimonials = testimonialService.GetAllTestimonials(); 

    rptTestimonials.DataSource = testimonials; 
    rptTestimonials.DataBind(); 
} 
+0

直到我如此接近。你在谈论什么是yourContextObject。那是我在课堂上提到的CMSContextObject吗? – 2012-04-25 18:31:29

+0

这是一个CMSObjectContext对象。您的TestimonialService类的构造函数期望它作为参数。因为我不知道你的代码,所以我不能告诉你从哪里得到这些信息。如果您无法访问Bind_Data中的当前CMSObjectContext对象,则必须以某种方式使其可用,或者您需要更改您的TestimonialService类以单独获取上下文,而不是以param方式传入。哪种解决方案是首选取决于多种因素,这些因素无法通过堆栈溢出中的几个代码片段进行评估。 – Till 2012-04-26 08:46:20

相关问题