2010-09-30 22 views
1

這已經涵蓋了幾次,沒有合適的答案:如何阻止我的ObjectDataSource綁定兩次?

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

我已經創建了一個用於與一個ObjectDataSource自定義分頁數據類。在初始測試中,我發現它的性能比我以前的SqlDataSource代碼差。在調查的同時,我發現對於每一次頁面加載,都會創建ObjectDataSource並綁定兩次。

調查上面的鏈接使我相信,這可能在問候是一個錯誤(或無法解釋的行爲),以改變我的GridView的列知名度在OnDataBound事件,像這樣:

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停止綁定兩次。

我真的不能想辦法解決這個問題。

有沒有其他人看到這個問題或制定了一項工作?

+0

如果您評論可見性問題,問題是否會持續存在? – citronas 2010-09-30 16:10:54

回答

2

爲什麼要更改RowDataBound事件中列的可見性?將爲您綁定的每個項目調用此事件。

在GridView上實際執行DataBind()調用之前,隱藏colums。

如果這對您沒有幫助,您將需要提供您用於數據綁定的代碼。

+0

我不是手動數據綁定它。它在頁面加載時自動進行數據綁定。我不知道什麼時候可以這樣做,除非我去了OnInit。另外,我在OnDataBound中做了很多,然後在這裏展示。我添加了JavaScript並調整了某些字段的顯示,所以我不確定這是否實用。 – clifgriffin 2010-09-30 15:07:27

+0

我編輯了原始問題以包含完整的代碼。 – clifgriffin 2010-09-30 15:11:33

+0

我給你這個信用。我結束了改變我的column.visible引用CSS更改:gvContacts.Columns [3] .ItemStyle.CssClass =「visible」;奇蹟般有效。謝謝! – clifgriffin 2010-10-01 14:35:07