2015-12-25 145 views
0

預先感謝您,請原諒此問題是否已被回答。item_databound正在兩次觸發

我想顯示記錄數。我在item_databound中放了一個計數器,注意到計數器是顯示記錄的兩倍。

我有一個數據訪問層處理數據網格爲:

Case "DataGrid" 
      If Len(DataValueField) > 0 Then 
       Dim oDS As New DataSet 
       Dim oDV As New DataView 
       oDV = DataTable.DefaultView 
       oDV.Sort = DataValueField & " ASC" 
       oDS.Tables.Add(oDV.ToTable) 
       CType(WebControl, DataGrid).DataSource = oDS 
       CType(WebControl, DataGrid).DataBind() 
      Else 
       CType(WebControl, DataGrid).DataSource = _DataSet 
       CType(WebControl, DataGrid).DataBind() 
      End If 
      CType(WebControl, DataGrid).DataSource = _DataSet 
      CType(WebControl, DataGrid).DataBind() 

如果我使用自己Item_Databound在頁面上此代碼只運行一次:

 Dim oDA As New SqlDataAdapter(SQL, _oConn) 
     Dim oDS As New DataSet 
     oDA.Fill(oDS) 
     grdUsers.DataSource = oDS 
     grdUsers.DataBind() 

我在做什麼錯在我的DAL那是導致兩次發射?

感謝, Fudd語

回答

0

有一個問題,顯然對你的代碼。 在

If Len(DataValueField) > 0 Then 

你如果語句完成後再次結合的三夏首次與oDS.And再次要綁定你_DataSet的WebControl。 這背後的邏輯是什麼?你想通過這個實現什麼?

+0

對不起,不是更清晰。當我使用oDS運行數據集的代碼時,我沒有使用DAL代碼,即我創建的目標代碼。 – Stelmerfudd

0

對不起,不太清楚。當我使用數據集時,我只能通過DataBound一次。使用該對象獲取它通過DataBound兩次的數據。這裏有什麼問題?謝謝。

下面是調用該對象代碼的網頁本身代碼:

Dim Query As New FencelineDAL.Query(_oConn) 
Query.CommandText = "[SELECT statement]" 
Query.DataBind(grdUsers) 

這裏是我認爲導致了兩遍對象:

Public Function DataBind() As Object 
Select Case WebControl.GetType().Name 
Case "DataGrid" 
     If Len(DataValueField) > 0 Then 
      Dim oDS As New DataSet 
      Dim oDV As New DataView 
      oDV = DataTable.DefaultView 
      oDV.Sort = DataValueField & " ASC" 
      oDS.Tables.Add(oDV.ToTable) 
      CType(WebControl, DataGrid).DataSource = oDS 
      CType(WebControl, DataGrid).DataBind() 
     Else 
      CType(WebControl, DataGrid).DataSource = _DataSet 
      CType(WebControl, DataGrid).DataBind() 
     End If 
End Select 
     CType(WebControl, DataGrid).DataSource = _DataSet 
     CType(WebControl, DataGrid).DataBind() 
    Return Webcontrol