Gridview的Rowdatabind发射了两次

时间:2011-12-01 14:47:46

标签: asp.net .net vb.net gridview

我有一个gridview并使用RowDataBound添加一行。这出现了两次。所以我打印出e.Row.RowType,它显示“header header dataRow dataRow footer footer” 它被解雇了两次。为什么这个事件会发生两次火灾?

<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

2 个答案:

答案 0 :(得分:2)

只需从html页面中删除RowDataBound。不需要在gridview tag.it中提及它。它会正常工作,因为它会被触发一次。

答案 1 :(得分:1)

RowDataBound逻辑放在if (e.Row.RowType == DataControlRowType.DataRow)

e.g。

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
相关问题