gpt4 book ai didi

c# - 找不到 ObjectDataSource 的 TypeName 属性中指定的类型

转载 作者:太空狗 更新时间:2023-10-29 20:49:14 24 4
gpt4 key购买 nike

由于需要,所有网站的.cs文件都存放在App_Code目录下,编译成App_Code.dll。当我尝试访问网站的特定页面时出现错误。

Description: An unhandled exception occurred during the execution
of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The type
specified in the TypeName property of ObjectDataSource
'DataSourceSubmissionList' could not be found.

我有一个由 ObjectDataSource 填充的 Gridview 控件。代码如下:

/layouts/Portal/Company/Application/code.ascx:

<%@ Control Language="c#" AutoEventWireup="true"
CodeFile="~/layouts/Portal/Company/Application/code.ascx.cs"
Inherits="Project.WebUserControls.myapplications.sublayout" %>

<dx:ASPxGridView ID="ASPxGridView1"
runat="server"
DataSourceID="DataSourceSubmissionList"
KeyFieldName="SubmissionId"
</dx:ASPxGridView>

<asp:ObjectDataSource
ID="DataSourceSubmissionList"
runat="server"
TypeName="Project.WebUserControls.myapplications.sublayout">
</asp:ObjectDataSource>

/layouts/Portal/Company/Application/code.ascx.cs:

namespace Project.WebUserControls.myapplications
{
public partial class sublayout: System.Web.UI.UserControl
{
}
}

当我在 code.ascx 文件中使用这一行来获取完全限定的类型名时...

<% Response.Write(typeof(Project.WebUserControls.myapplications.sublayout).AssemblyQualifiedName); %>

它把这个打印在页面上。

Project.WebUserControls.myapplications.sublayout, App_Web_oiftguk4,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

但是,在 ObjectDataSource TypeName 中使用此确切类型 (Project.WebUserControls.myapplications.sublayout) 会导致错误。

我已经解决了很多关于这个错误信息的问题。我读过可能的解决方案是在 TypeName 属性 like this 中同时使用命名空间和程序集名称.但我不能那样做,因为代码是动态编译的,程序集名称不断变化。

另一件事 - 只有在使用 CodeFile 方法时才会发生此错误。如果我切换到 CodBehind,没问题。

此行为的原因可能是什么?

最佳答案

我通过在 Page_Init 期间初始化 TypeName 属性而不是直接在 ObjectDataSource 中指定 TypeName 解决了这个问题:

/layouts/Portal/Company/Application/code.ascx.cs:

public void Page_Init(object o, EventArgs e)
{
DataSourceSubmissionList.TypeName = this.GetType().AssemblyQualifiedName;
}

/layouts/Portal/Company/Application/code.ascx:

<asp:ObjectDataSource
ID="DataSourceSubmissionList"
runat="server"
</asp:ObjectDataSource>

关于c# - 找不到 ObjectDataSource 的 TypeName 属性中指定的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20328819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com