views:

73

answers:

1

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

  1. http://stackoverflow.com/questions/2641156/objectdatasource-firing-twice-or-on-its-own
  2. http://stackoverflow.com/questions/651293/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 != "&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);
            }
        }
    }
}

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?

A: 

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.

citronas
I'm not manually data binding it. It databinds automagically when the page loads. I'm not sure when I could do this unless I went to OnInit. Also, I'm doing a lot more in OnDataBound then I show here. I'm adding javascript and tweaking display of certain fields, so I'm not sure that would be practical.
clifgriffin
I edited the original question to include the full code.
clifgriffin
I'm giving you credit for this. I ended up changing my column.visible references to CSS changes: gvContacts.Columns[3].ItemStyle.CssClass = "visible"; Works like a charm. Thanks!
clifgriffin