如何以编程方式在 ASP.Net 中连接 LINQ 数据源?

发布于 2024-08-14 05:10:37 字数 1431 浏览 2 评论 0原文

myGridView.DataSource = LinqDataSource 有效,但仅适用于选择。我有编辑和删除列,当我尝试使用它们时,我收到有关事件未被捕获的错误。具体来说,我已经看到了 OnRowDeleting,但我确信还有其他需要连接。

myGridView.OnRowDeleting = ??

我似乎在 LinqDataSource 上找不到任何看起来像我需要的东西:(

编辑:这里有一些示例代码说明了我正在做的事情。

protected virtual void OnRowDeleted(Object sender, GridViewDeletedEventArgs e)
{
    // it means the last row was deleted
    if (fieldGridView.Rows.Count == 1)
    {
        fieldGridView.DataSourceID = null;
        fieldGridView.DataSource = new[] { new FormField { Required = false } };
        fieldGridView.AutoGenerateDeleteButton = false;
        fieldGridView.AutoGenerateEditButton = false;
    }
}

protected void InsertButton_Click(object sender, CommandEventArgs e)
{
    // pull data out of footer row and insert it into the DB
    if (fieldGridView.DataSource == null || fieldGridView.DataSource.GetType() != LinqDataSource1.GetType())
    {
        fieldGridView.DataSource = LinqDataSource1;
        fieldGridView.AutoGenerateDeleteButton = true;
        fieldGridView.AutoGenerateEditButton = true;
    }
}

另请注意,我已在标记和中静态设置了 OnRowDeleting 事件错误消失了。但是,代码正在更新它,它只是由于某种原因而保留,所以当我重新启用删除列时,数据源最终仍然存在。我分配给它的临时数据源(这只是一个列表),所以当我点击删除时它会爆炸,因为我现在静态定义了回调,所以它被调用,但没有删除发生,因为数据源不是。没有正确切换回 linq 数据源,

所以本质上,我的问题是,当我动态将数据源设置为上次删除时的列表时,更改会保留,但是当我动态将数据源设置为 linq 时。第一次插入时的数据源,更改没有粘住,最奇怪的部分是我所做的其他更改确实粘住了,只是数据源没有粘住。我正在启用自动生成的编辑和删除列,并且该更改会立即反映出来。

myGridView.DataSource = LinqDataSource works but only for select. I have edit and delete columns and when I try to use them I get errors about events not getting caught. Specifically I've seen OnRowDeleting, but I'm sure there are others that need to be wired up.

myGridView.OnRowDeleting = ??

I can't seem to find anything on the LinqDataSource that looks like what I need :(

edit: here is some some sample code illustrating what I'm doing.

protected virtual void OnRowDeleted(Object sender, GridViewDeletedEventArgs e)
{
    // it means the last row was deleted
    if (fieldGridView.Rows.Count == 1)
    {
        fieldGridView.DataSourceID = null;
        fieldGridView.DataSource = new[] { new FormField { Required = false } };
        fieldGridView.AutoGenerateDeleteButton = false;
        fieldGridView.AutoGenerateEditButton = false;
    }
}

protected void InsertButton_Click(object sender, CommandEventArgs e)
{
    // pull data out of footer row and insert it into the DB
    if (fieldGridView.DataSource == null || fieldGridView.DataSource.GetType() != LinqDataSource1.GetType())
    {
        fieldGridView.DataSource = LinqDataSource1;
        fieldGridView.AutoGenerateDeleteButton = true;
        fieldGridView.AutoGenerateEditButton = true;
    }
}

Also note that I've statically set the OnRowDeleting event in the markup and the error went away. However, the Linq data source is not getting set back properly. The code is updating it, it's just sticking for whatever reason, so what's happening is that when I re-enable the delete column the data source ends up still being the temp data source I assigned to it (which is just a list), so when I hit delete it was blowing up. Since I've now statically defined the callback, it gets called, but no delete happens because the data source isn't being switched back to the linq data source properly.

so in essence, my issue is that when I dynamically set the data source to the list on the last delete, the change is sticking, but when I dynamically set the data source to the linq data source on the first insert, the change is not sticking. The strangest part of it is that other changes I'm making do stick, just not the data source. I'm enabling the autogenerated edit and delete columns and that change is being reflected immediately.

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

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

发布评论

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

评论(2

乖乖 2024-08-21 05:10:37

在您的 linq 数据源上,您是否记得显式设置删除和插入的启用标志?

<asp:LinqDataSource ID="MyLinqDataSource" EnableDelete="true" EnableUpdate="true" EnableInsert="true" runat="server" ....

On your linq datasource, did you remember to explicitly set the enable flags for delete and insert?

<asp:LinqDataSource ID="MyLinqDataSource" EnableDelete="true" EnableUpdate="true" EnableInsert="true" runat="server" ....
浅浅 2024-08-21 05:10:37

最终我无法解决这个具体问题,但我确实找到了解决更大问题的方法。

本质上,我所做的是创建两个具有不同数据源的网格视图,为每行创建一个自定义删除按钮,然后相应地交换网格。这不完全是一个很好的解决方案,但它足以满足我正在做的事情。

至于为什么我原来的交换数据源的解决方案不起作用,我不知道。我参与的大多数项目都使用非 MS 工具来处理表格数据,所以我什至不知道该去哪里寻找。但是,如果有人对我为何遇到这种行为有任何好的想法,我会洗耳恭听。

Ultimately I was not able to fix this specific issue, but I did find a workaround for my larger issue.

In essence what I've done is to create two gridviews with different data sources, created a custom delete button for each row, and then swap the grids out accordingly. Not exactly a great solution, but it suffices for what I'm doing.

As for why my original solution of swapping the datasources doesn't work, I don't know. Most of the projects I've been on have used non-MS tools for tabulated data so I'm not even sure where to look. However, if anyone has any good ideas as to why I've encountered this behavior, I'm all ears.

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