DotNetNuke:未找到 GridView 的 ObjectDataSource

发布于 2024-10-13 18:53:51 字数 2980 浏览 1 评论 0原文

作为关于 GridView 和 DotNetNuke 的上一个问题的后续内容,我在让事情正确运作方面遇到了一些麻烦。现在,我的 ascx 文件中有一个简单的 GridView,并将数据绑定到 .cs 文件中的 GridView,如下所示:

DiscoveryController objDiscoverys = new DiscoveryController();
List<DiscoveryInfo> lstDiscoveries = objDiscoverys.GetDiscoverys(ModuleId);

grdDiscoverys.DataSource = lstDiscoveries;
grdDiscoverys.DataBind();

这有效。但是,我在 教程 中看到了另一种方法,它定义了 < code>在控制器中,这似乎允许设计器做更智能的事情,例如通过复选框添加功能删除按钮。在本教程的后面,我看到内联编辑也正在完成,这是我想要的功能。

所以我决定尝试一下。也就是说:

<asp:ObjectDataSource ID="objDataSource" runat="server" TypeName="MyCompany.Modules.Discovery.DiscoveryController" />

由于 bin 文件夹中的 dll 文件名为 MyCompany.Modules.Discovery(它与我在 C# 项目中设置的程序集名称和默认命名空间相匹配),因此这是非常有意义的。正如教程所述,然后我使用设计器尝试将数据源绑定到 GridView。

但是,我收到一条错误消息,无法加载它。命名空间名称和类名称匹配,并且我可以从代码隐藏中清楚地绑定它,那么给出了什么?

编辑:后续行动。经过一些实验,我发现虽然设计器看不到我的模块,但 .ascx 模板本身可以。将其放入我的 .ascx 文件中似乎可以工作...在大多数情况下:

<asp:ObjectDataSource ID="objDataSource" runat="server" TypeName="MyCompany.Modules.Discovery.DiscoveryController" SelectMethod="GetDiscoverys" UpdateMethod="UpdateDiscovery" DeleteMethod="DeleteDiscovery">
    <SelectParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </SelectParameters>
    <UpdateParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </UpdateParameters>
    <DeleteParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="grdDiscoverys" runat="server" DataSourceID="objDataSource" EnableModelValidation="True" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" DataKeyNames="ItemId">
    <Columns>
        <asp:BoundField DataField="ItemId" HeaderText="#" ReadOnly="true" />
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:BoundField DataField="Image" HeaderText="Image URL" />
        <asp:BoundField DataField="Link" HeaderText="Link" />
    </Columns>
</asp:GridView>

太棒了...看起来我已经镜像了设计器将添加的大部分功能...除了一个很小的小东西。自动更新不更新。

更具体地说,当我尝试更新字段时,我收到此消息:

ObjectDataSource 'objDataSource' could not find a non-generic method 'UpdateDiscovery' that has parameters: ModuleId, Title, Image, Link, ItemId.

当然它不起作用!方法签名是这样的:

public void UpdateDiscovery(DiscoveryInfo objDiscovery)

此时,我已经非常接近让某些东西工作起来了,我可以尝尝它的味道,DAL——该死的,我要改变这个函数,所以它需要这五个确切的参数,而不是一个数据对象。然而,我上面引用的教程似乎以某种方式说服自动更新传递数据对象,所以我很好奇它是如何逃脱的。

As a follow-up to a previous question about GridView and DotNetNuke, I'm having a little more trouble getting things to act correctly. Right now I have a simple GridView in my ascx file and I bind the data to the GridView in my .cs file like so:

DiscoveryController objDiscoverys = new DiscoveryController();
List<DiscoveryInfo> lstDiscoveries = objDiscoverys.GetDiscoverys(ModuleId);

grdDiscoverys.DataSource = lstDiscoveries;
grdDiscoverys.DataBind();

This works. However, I have seen an alternate method in a tutorial which instead defines an <asp:ObjectDataSource> in the controller, and this seems to allow the Designer to do more intelligent things, such as add functioning Delete buttons through a checkbox. Later on in the tutorial, I see inline editing being done as well, which is functionality I desire.

So I decided to give it a shot. To wit:

<asp:ObjectDataSource ID="objDataSource" runat="server" TypeName="MyCompany.Modules.Discovery.DiscoveryController" />

As my dll file in the bin folder is named MyCompany.Modules.Discovery (which matches the assembly name and default namespace I have set up in my C# project), this makes perfect sense. As the tutorial says, I then used the Designer to attempt to bind the Data Source to the GridView.

However, I get an error message that it can't be loaded. The namespace names and class name match up, and I can clearly bind it from the codebehind, so what gives?

EDIT: A follow up. After some experimentation, I have discovered that while the Designer cannot see my module, the .ascx template itself can. Putting this in my .ascx file seems to work...for the most part:

<asp:ObjectDataSource ID="objDataSource" runat="server" TypeName="MyCompany.Modules.Discovery.DiscoveryController" SelectMethod="GetDiscoverys" UpdateMethod="UpdateDiscovery" DeleteMethod="DeleteDiscovery">
    <SelectParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </SelectParameters>
    <UpdateParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </UpdateParameters>
    <DeleteParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="grdDiscoverys" runat="server" DataSourceID="objDataSource" EnableModelValidation="True" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" DataKeyNames="ItemId">
    <Columns>
        <asp:BoundField DataField="ItemId" HeaderText="#" ReadOnly="true" />
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:BoundField DataField="Image" HeaderText="Image URL" />
        <asp:BoundField DataField="Link" HeaderText="Link" />
    </Columns>
</asp:GridView>

Fantastic...it looks like I've mirrored most of the functionality of what the Designer would have added...except for one teensy little thing. The automatic update doesn't update.

More specifically, I get this message when I try to update a field:

ObjectDataSource 'objDataSource' could not find a non-generic method 'UpdateDiscovery' that has parameters: ModuleId, Title, Image, Link, ItemId.

Of course it doesn't work! The method signature goes like this:

public void UpdateDiscovery(DiscoveryInfo objDiscovery)

At this point, I'm so close to getting something working that I can taste it, and DAL-be-damned I'm about to change the function so it takes those five exact params instead of a data object. However, the tutorial that I referenced above seemed to somehow convince the automatic update to pass a data object, so I'm kind of curious to know how it got away with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-10-20 18:53:51

更改对象数据源声明以匹配您的更新方法:

public void UpdateDiscovery(DiscoveryInfo objDiscovery)

在更新参数声明中使用单个参数:

<UpdateParameters>
        <asp:Parameter Name="objDiscovery" />
    </UpdateParameters>

use object data source's updating event to create an object from the existing control set and assign it to parameter's default value.

祝你好运

Change your object data source declaration to match your update method:

public void UpdateDiscovery(DiscoveryInfo objDiscovery)

Use single parameter in update parameter declaration:

<UpdateParameters>
        <asp:Parameter Name="objDiscovery" />
    </UpdateParameters>

use object data source's updating event to create an object from the existing control set and assign it to parameter's default value.

Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文