简体   繁体   English

为什么页面回发时会触发GridView.RowCreated事件

[英]Why GridView.RowCreated event is fired when page postback

All, I was using the RowCreated event to fill content for the every Row of the GridView . 所有,我都使用RowCreated事件来填充GridView的每一Row的内容。 But I found the RowCreated event of GridView is triggered whenever I click any server control like Asp.net Button which could trigger the page postback. 但是我发现,每当我单击任何可能会触发页面回发的服务器控件(如Asp.net Button ,都会触发GridViewRowCreated事件。 And it will cause a problem which the e.Row.DataItem is not available(null). 这将导致e.Row.DataItem不可用(空)的问题。

The code simply looks like below. 该代码如下所示。

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SomeObject mapItem = (SomeObject)e.Row.DataItem;//DataItem is null when postback.
        }         
    }

And I am sure I didn't bind anything DataTable or List<T> with the GridView when postback. 而且我确定回发时没有将任何DataTableList<T>GridView绑定。 After researched. 经过研究。 I also found if I move the code to RowDataBound event. 我还发现是否将代码移至RowDataBound事件。 everything is ok. 一切都好。 the problem is gone. 问题解决了。 I just don't know why. 我只是不知道为什么。 Hope someone can help me. 希望可以有人帮帮我。 thanks. 谢谢。

As per MSDN , 根据MSDN

Before the GridView control can be rendered, a GridViewRow object must be created for each row in the control. 在可以呈现GridView控件之前,必须为控件中的每一行创建一个GridViewRow对象。 The RowCreated event is raised when each row in the GridView control is created. 在GridView控件中创建每一行时,都会引发RowCreated事件。 This enables you to provide an event-handling method that performs a custom routine, such as adding custom content to a row, whenever this event occurs. 这使您能够提供一个事件处理方法,该方法将在发生此事件时执行自定义例程,例如将自定义内容添加到行中。

Also check below link that explains why it happens: 还要检查下面的链接,该链接解释了为什么会发生这种情况:

GridView event raise unexpectedly after postback 回发后GridView事件意外引发

RowCreated is focused on parsing Gridview row's definition and creating the Gridview row's control structure, RowCreated专注于解析Gridview行的定义并创建Gridview行的控件结构,

RowDataBound is focused on bind data to the row's controls created in the RowCreated. RowDataBound专注于将数据绑定到在RowCreated中创建的行的控件。

Also RowCreated is invoked automatically in both init and postback case, but RowDataBound is only invoke when the DataBind is called. 同样,在初始化和回发两种情况下,都会自动调用RowCreated,但是只有在调用DataBind时才调用RowDataBound。

Every time your grid has bind data, the RowCreated event will call each time. 每次网格绑定数据时,都会每次调用RowCreated事件。

Have you bind gridview on page load? 您是否在页面加载时绑定了gridview? If this is your case, the RowCreated event will fire in every postback. 如果是这种情况,RowCreated事件将在每次回发中触发。 To avoid this, check !IsPostback to control it and manage your gridview binding. 为了避免这种情况,请检查!IsPostback以控制它并管理您的gridview绑定。 In in most cases, everyone (including Microsoft) raccomend to use RowDataBound event instead of RowCreated. 在大多数情况下,每个人(包括Microsoft)都鼓励使用RowDataBound事件而不是RowCreated事件。

Hope to be helpful :) 希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM