2011-12-01 60 views
0

我有一個gridview並使用RowDataBound添加一行。這出現兩次。所以我打印出e.Row.RowType,它顯示「標題頭dataRow dataRow頁腳頁腳」 它被解僱了兩次someason。爲什麼此事件發生兩次?Rowdatabind gridview發射兩次

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" EmptyDataText="There is nothing in your shopping cart." GridLines="None" ShowFooter="true" DataKeyNames="id" OnRowDataBound="gv_RowDataBound" EnableModelValidation="True" > 
     <Columns> 
     <asp:TemplateField HeaderText="Item" SortExpression="name"> 
      <ItemTemplate> 
       <asp:Label ID="ItemContentId" runat="server" Text='<%# Eval("publicationContentId").ToString() %>'></asp:Label> 
       <asp:Label ID="ItemField" runat="server" Text='<%# Eval("name").ToString() %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 

    </Columns> 
</asp:GridView> 


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
    If Not Page.IsPostBack Then 
      BindData()   
    End If 
End Sub 

Protected Sub BindData() 

    gv.DataSource = ShoppingCart.Instance.Items 
    gv.DataBind() 


End Sub 


Protected Sub gvPublications_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvPublications.RowDataBound 
    Response.Write(e.Row.RowType.ToString() + "<br/>") 


' Dim row2 As New GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal) 
    ' For i As Integer = 0 To TotalItems 
    '  Dim emptyCell As New TableCell 
    '  row2.Cells.Add(emptyCell) 
    ' Next 

    ' total = 10 

    ' row2.Cells(lastIndex).Text = "<strong>" + DisplayMoney(total.ToString()) + "</strong>" 
    ' gv.Controls(0).Controls.Add(row2) 
End Sub 

回答

1

把你RowDataBound邏輯if (e.Row.RowType == DataControlRowType.DataRow)

例如下

Protected Sub gvPublications_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvPublications.RowDataBound 
if (e.Row.RowType == DataControlRowType.DataRow) 

Response.Write(e.Row.RowType.ToString() + "<br/>") 


' Dim row2 As New GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal) 
' For i As Integer = 0 To TotalItems 
'  Dim emptyCell As New TableCell 
'  row2.Cells.Add(emptyCell) 
' Next 

' total = 10 

' row2.Cells(lastIndex).Text = "<strong>" + DisplayMoney(total.ToString()) + "</strong>" 
' gv.Controls(0).Controls.Add(row2) 
End Sub 
+0

我有arealdy使用下面的聲明。 「if(e.RowType == DataControlRowType.DataRow)」在我打印出行類型之後,它仍然顯示「頁腳頁腳」。 – jeeunit02

2

剛剛從HTML刪除的RowDataBound page.No需要提到它在GridView tag.it將正常工作,因爲它會被觸發一次的。