1

イベントでボタンを動的に作成すると、asp.net 4.5 でエラーが発生しますGridView.ROWDATABOUND

ボタンのコード:

Dim btn1 As New Button()
btn1.ID = "btn1"
btn1.Width = "50"
btn1.Text = "Edit"
btn1.CssClass = "AdminPageBtn"
btn1.CommandName = "Edit"
btn1.CommandArgument = "Edit"
btn1.Enabled = True

e.Row.Cells(e.Row.Cells.Count - 1).Controls.Add(btn1)

また、スクリプト マネージャーと更新パネルを備えた AJAX 4.0 も使用しています。

そのボタンは以下のメソッドを実行します

Protected Sub gvwRoutes_RowEditing(ByVal sender As Object, 
                      ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
                Handles gvwRoutes.RowEditing
End Sub

実行すると、エラーが発生します。

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

解決策が必要..!

4

1 に答える 1

2

コントロール コレクションの途中にコントロールを追加しようとすると、間違いなくビュー ステートの問題が発生します。Asp.net の動的コントロールの MSDN仕様に従って

Inserting a dynamic control somewhere other than the end of the Controls collection 
can cause a corrupted view state

これに対する可能な解決策はPlaceholder、動的コントロールを追加できるページで を使用することです。これにより、コントロール コレクションの途中でインデックスが予約され、ビューの状態も処理されます。新しく追加されたコントロールには、プレースホルダー コントロールからのビュー ステートがあります。

于 2013-10-14T11:26:51.987 に答える