【问题标题】:gridview in web forms Could not load typeWeb 表单中的 gridview 无法加载类型
【发布时间】:2016-11-17 13:28:33
【问题描述】:

我有一个 ASP.NET、WebForms Web 应用程序,并且我正在使用实体框架。
我需要将一些数据从 DB 放到 GridView,但是当我将最基本的代码添加到 GridView 的 List.aspx 页面时,我收到错误消息“无法加载类型'报告'”错误:

<asp:GridView ID="gvReports" runat="server" ItemType="Report" SelectMethod="gvReports_GetData"></asp:GridView>

我正在使用的包:

package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />

Entity 的模型如下所示:

SelectMethod 看起来像这样:

public IQueryable<Report> gvReports_GetData()
    {
        FinReports_ConceptEntities db = new FinReports_ConceptEntities();
        return db.Reports;
    }

编辑我按照这个教程 https://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

【问题讨论】:

  • 你能显示报表模型类吗?
  • 是的,但我不明白这有什么关系。类报告是通过数据库优先模型从实体自动生成的,并且没有被编辑,并且 GridView 应该自动为该类生成列,因为 AutomaticalyGenerateColumns 为真。简而言之,一切都是自动生成的,我不可能在这些自动生成的组件的代码隐藏中犯错误。
  • 将 AutoGenerateColumns="True" 属性添加到您的网格视图。您还可以在 ItemType="ProjectName.FolderName.Report" 中添加命名空间
  • 我也犯了同样的错误。我确实相信这些变量有一些默认值,如果没有提及它们,它们将具有该默认值,因此如果 AutoGenerateColumns 的默认值已经为 true,则无需说 AutoGenerateColumns="True"。
  • @mww 说使用ProjectName.FolderName.Report 是一种误导。您应该使用命名空间,它并不总是与ProjectName.FolderName 对齐。

标签: c# asp.net gridview


【解决方案1】:

发生这种情况是因为您的页面上没有正确引用命名空间。

有三种解决方案:


为您的项目类型使用完全限定的名称。

<asp:GridView ID="gvReports" runat="server" ItemType="MyNamespace.Report" SelectMethod="gvReports_GetData"></asp:GridView>

使用导入命名空间指令在页面顶部导入命名空间(类似于 C# 类中的 using 语句)。见How to add namespace in aspx file?

<%@ Import Namespace="MyNamespace" %>

或者您可以通过向web.config 文件添加一些配置来导入项目中所有 ASPX 页面的命名空间。见How to add namespaces in web.config file?

<configuration>
  <system.web>
    <pages>
      <namespaces>
        <add namespace="MyNamespace" />
      </namespaces>
    </pages>  
  </system.web>
</configuration>

请注意,完全限定它时它不是MyProject.MyClass 语法。它是MyNamespace.MyClass。碰巧您的项目和命名空间是相同的,但情况并非总是如此。

【讨论】:

    【解决方案2】:

    解决方案是在 ItemType 属性中,而不是我的班级名称(报告),我应该放置该班级的完整路径(your_project_name.your_class_name)。上面列出的教程中没有提到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多