如何阻止我的 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 != " ")
{
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要更改 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.