Rowdatabind gridview发射两次

问题描述:

我有一个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 

把你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

刚刚从HTML删除的RowDataBound page.No需要提到它在GridView tag.it将正常工作,因为它会被触发一次的。