【问题标题】:Cannot access non-static field "Repeater" in static context无法在静态上下文中访问非静态字段“Repeater”
【发布时间】:2014-10-08 19:04:20
【问题描述】:

我在一个使用中继器的 Web 表单项目中有一个网页;这是页面上的代码:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div class="Event">
            <div class="">
                <h3><a href="<%# DataBinder.Eval(Container.DataItem, "Url") %>"><%# DataBinder.Eval(Container.DataItem, "Name") %></a></h3>
                <span><%# DataBinder.Eval(Container.DataItem, "Location") %>. <%# DataBinder.Eval(Container.DataItem, "DateDisplay") %></span>
                &nbsp;
            </div>

        </div>
    </ItemTemplate>
</asp:Repeater>

这是代码隐藏:

    namespace TestWebForms
{
    public partial class EventSearchResults : System.Web.UI.Page
    {
        public string searchTerms { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var query = HttpContext.Current.Request.QueryString;

            var searchTerms = String.IsNullOrEmpty(query["q"]) ? String.Empty : query["q"]; 

            ArrayList values = new ArrayList();

            values.Add(new Event()
            {
                Name = "Class of 1715 300th Reunion",
                Location = "Cambridge, MA",
                DateDisplay = "May 20th 2015",
                Url = "#"
            });
            values.Add(new Event()
            {
                Name = "Symposium on Proper Cat Petting Techniques",
                Location = "Boston, MA",
                DateDisplay = "December 25th 2014",
                Url = "#"
            });
            values.Add(new Event()
            {
                Name = "Puppies Everywhere!!!",
                Location = "Cambridge, MA",
                DateDisplay = "November 12th 2014",
                Url = "#"
            });

            Repeater1.DataSource = values;
            Repeater1.DataBind();

        }
    }
}

效果很好。

然后我将所有内容复制并粘贴到一个新项目中的文件中(长话短说,但基本上我需要文件存在于项目 2 中,但我无法查看项目 2 中的站点,所以我创建了我需要的页面在项目 1 中查看它,然后在完成后将其复制到项目 2)

文件在字面上完全相同(网页的命名空间除外)。但是中继器在项目 2 中不起作用。在代码隐藏中,Repeater1 有一个红色下划线,并显示“无法在静态上下文中访问非静态字段“Repeater”。”

我该如何解决这个问题,为什么它会在一个项目中起作用而在另一个项目中不起作用?

【问题讨论】:

  • 想不出为什么会导致该错误,但是您有 EventSearchResults.aspx.designer.cs 文件吗?如果有,您是否也更改了其中的命名空间?
  • 不,我没有那个文件

标签: html .net static repeater httpcontext


【解决方案1】:

我想通了,当我将它复制到一个新文件时,创建的新文件使用的是“CodeFile”而不是“CodeBehind”。将其更改为 CodeBehind 解决了问题

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EventSearchResults.aspx.cs" Inherits="WebAssets_Templates_EventSearchResults" %>

【讨论】:

  • 您可以通过单击“勾选”来“接受”您自己的答案。这从未回答的问题池中删除了该问题。
猜你喜欢
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2017-03-03
  • 2010-10-23
  • 2017-08-19
相关资源
最近更新 更多