title: How do I stop my ObjectDataSource from binding twice?
body: <p>This has been covered a couple of times, without a suitable answer:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/2641156/objectdatasource-firing-twice-or-on-its-own">ObjectDataSource firing twice, or on its own</a></li>
  <li><a href="https://stackoverflow.com/questions/651293/objectdatasource-created-twice-when-control-is-changed">ObjectDataSource created twice when control is changed</a></li>
  </ol>
  
  <p>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. </p>
  
  <p>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:</p>
  
  <pre><code>protected void gvContacts_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType != DataControlRowType.Pager &amp;&amp; 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 != "&amp;nbsp;")
          {
              e.Row.Cells[4].Text = "&lt;a href=\"mailto:" + e.Row.Cells[4].Text + "\"&gt;" + e.Row.Cells[4].Text + "&lt;/a&gt;";
          }
          //Birthday highlight
          if (e.Row.Cells[6].Text != "&amp;nbsp;")
          {
              DateTime dt = Convert.ToDateTime(e.Row.Cells[6].Text);
              DateTime now = DateTime.Now;
              if (dt.Day == now.Day &amp;&amp; dt.Month == now.Month)
              {
                  e.Row.Cells[6].BackColor = System.Drawing.Color.FromArgb(255, 230, 160);
              }
          }
      }
  }
  </code></pre>
  
  <p>I use this event to customize the display of certain fields, as well as hide columns that are not applicable during some search types. </p>
  
  <p>When I disable the event, the ODS stops binding twice. </p>
  
  <p>I can't really think of a way to get around this behavior. </p>
  
  <p>Has anyone else see this issue or developed a work around?</p>