0

すべて、私はイベントを使用して、のすべてのRowCreatedコンテンツを埋めていました。しかし、ページのポストバックをトリガーする可能性のあるサーバーコントロールをクリックすると、イベントがトリガーされることがわかりました。そして、利用できない(null)という問題が発生します。RowGridViewRowCreatedGridViewAsp.net Buttone.Row.DataItem

コードは単純に以下のようになります。

    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.
        }         
    }

DataTableそして、私は何かをバインドしたりList<T>GridViewwhen ポストバックを使用したりしなかったと確信しています。調べた次第。RowDataBoundコードをイベントに移動した場合もわかりました。全て大丈夫。問題はなくなりました。理由はわかりません。誰かが私を助けてくれることを願っています。ありがとう。

4

2 に答える 2

2

MSDNに従って、

GridView コントロールをレンダリングする前に、コントロールの行ごとに GridViewRow オブジェクトを作成する必要があります。GridView コントロールの各行が作成されると、RowCreated イベントが発生します。これにより、このイベントが発生するたびに、カスタム コンテンツを行に追加するなどのカスタム ルーチンを実行するイベント処理メソッドを提供できます。

また、それが発生する理由を説明する以下のリンクも確認してください。

ポストバック後に GridView イベントが予期せず発生する

于 2013-07-17T08:50:38.557 に答える
1

RowCreated is focused on parsing Gridview row's definition and creating the Gridview row's control structure,

RowDataBound is focused on bind data to the row's controls created in the RowCreated.

Also RowCreated is invoked automatically in both init and postback case, but RowDataBound is only invoke when the DataBind is called.

Every time your grid has bind data, the RowCreated event will call each time.

Have you bind gridview on page load? If this is your case, the RowCreated event will fire in every postback. To avoid this, check !IsPostback to control it and manage your gridview binding. In in most cases, everyone (including Microsoft) raccomend to use RowDataBound event instead of RowCreated.

Hope to be helpful :)

于 2013-07-17T08:23:41.957 に答える