如何阻止我的 ObjectDataSource 绑定两次?

发布于 2024-09-25 21:57:35 字数 2405 浏览 1 评论 0原文

这个问题已经被讨论过几次了,但没有合适的答案:

  1. ObjectDataSource 触发两次,或单独使用
  2. 当控件更改时,ObjectDataSource 创建两次

我创建了一个与 ObjectDataSource 一起使用的自定义分页数据类。在最初的测试中,我发现它的性能比我旧的 SqlDataSource 代码更差。在调查时,我发现对于每个页面加载,ObjectDataSource 都会被创建和绑定两次。

调查上面的链接让我相信这可能是关于在 OnDataBound 事件中更改 GridView 的列可见性的错误(或无法解释的行为),如下所示:

protected void gvContacts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.Pager && e.Row.Cells[0].Text != gvContacts.EmptyDataText)
    {
        e.Row.Cells[0].Visible = false;
        if (Convert.ToInt16(lstSearchType.SelectedValue) == ADDRESS)
        {
            gvContacts.Columns[2].ItemStyle.Width = Unit.Percentage(30);
            gvContacts.Columns[3].Visible = true;
            gvContacts.Columns[3].ItemStyle.Width = Unit.Percentage(20);
        }
        else
        {
            gvContacts.Columns[2].ItemStyle.Width = Unit.Percentage(50);
            gvContacts.Columns[3].Visible = false;
        }
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["ID"] = "contact_" + e.Row.Cells[0].Text;
        e.Row.Attributes["onclick"] = "javascript:selectRow($(this).attr('id').replace('contact_',''),2);";
        e.Row.Attributes["ondblclick"] = "javascript:openContact($(this).attr('id').replace('contact_',''),''); selectRow($(this).attr('id').replace('contact_',''),2);";

        //E-mail link
        if (e.Row.Cells[4].Text != " ")
        {
            e.Row.Cells[4].Text = "<a href=\"mailto:" + e.Row.Cells[4].Text + "\">" + e.Row.Cells[4].Text + "</a>";
        }
        //Birthday highlight
        if (e.Row.Cells[6].Text != "&nbsp;")
        {
            DateTime dt = Convert.ToDateTime(e.Row.Cells[6].Text);
            DateTime now = DateTime.Now;
            if (dt.Day == now.Day && dt.Month == now.Month)
            {
                e.Row.Cells[6].BackColor = System.Drawing.Color.FromArgb(255, 230, 160);
            }
        }
    }
}

我使用此事件来自定义某些字段的显示以及隐藏列在某些搜索类型中不适用。

当我禁用该事件时,ODS 停止绑定两次。

我真的想不出一种方法来解决这种行为。

有其他人看到这个问题或开发出解决方法吗?

This has been covered a couple of times, without a suitable answer:

  1. ObjectDataSource firing twice, or on its own
  2. ObjectDataSource created twice when control is changed

I have created a custom paging data class that is used with an ObjectDataSource. In intial tests, I found it was performing worse than my old SqlDataSource code. Whilst investigating, I found that for every page load, the ObjectDataSource is being created and binding twice.

Investigating the links above led me to believe this could be a bug (or unexplained behavior) in regards to changing my GridView's column visibility in the OnDataBound event like so:

protected void gvContacts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.Pager && e.Row.Cells[0].Text != gvContacts.EmptyDataText)
    {
        e.Row.Cells[0].Visible = false;
        if (Convert.ToInt16(lstSearchType.SelectedValue) == ADDRESS)
        {
            gvContacts.Columns[2].ItemStyle.Width = Unit.Percentage(30);
            gvContacts.Columns[3].Visible = true;
            gvContacts.Columns[3].ItemStyle.Width = Unit.Percentage(20);
        }
        else
        {
            gvContacts.Columns[2].ItemStyle.Width = Unit.Percentage(50);
            gvContacts.Columns[3].Visible = false;
        }
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["ID"] = "contact_" + e.Row.Cells[0].Text;
        e.Row.Attributes["onclick"] = "javascript:selectRow($(this).attr('id').replace('contact_',''),2);";
        e.Row.Attributes["ondblclick"] = "javascript:openContact($(this).attr('id').replace('contact_',''),''); selectRow($(this).attr('id').replace('contact_',''),2);";

        //E-mail link
        if (e.Row.Cells[4].Text != " ")
        {
            e.Row.Cells[4].Text = "<a href=\"mailto:" + e.Row.Cells[4].Text + "\">" + e.Row.Cells[4].Text + "</a>";
        }
        //Birthday highlight
        if (e.Row.Cells[6].Text != " ")
        {
            DateTime dt = Convert.ToDateTime(e.Row.Cells[6].Text);
            DateTime now = DateTime.Now;
            if (dt.Day == now.Day && dt.Month == now.Month)
            {
                e.Row.Cells[6].BackColor = System.Drawing.Color.FromArgb(255, 230, 160);
            }
        }
    }
}

I use this event to customize the display of certain fields, as well as hide columns that are not applicable during some search types.

When I disable the event, the ODS stops binding twice.

I can't really think of a way to get around this behavior.

Has anyone else see this issue or developed a work around?

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

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

发布评论

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

评论(1

哑剧 2024-10-02 21:57:35

为什么要更改 RowDataBound 事件中列的可见性?将为您绑定的每个项目调用此事件。

在 GridView 上实际执行 DataBind() 调用之前隐藏列。

如果这对您没有帮助,您将需要提供用于数据绑定的代码。

Why do you want to change the visibilty of the columns in the RowDataBound Event? This event will be called for each item that you are binding.

Hide the colums before you actually perform the DataBind() call on the GridView.

If this does not help you, you will need to provide the code you use for databinding.

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