Monday, 2 September 2013

Dynamically added link button disappeared on button click leaving an empty column

Dynamically added link button disappeared on button click leaving an empty
column

The datagrid reads from several xml files, so I create the columns
dynamically, and added a templatefield as the last column.
A link button is added in the templatefield using RowDataBound.
Private Sub GridItem_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridItem.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim linkb As New LinkButton
linkb.Text = "Delete"
linkb.ID = "LinkDeleteItem"
linkb.OnClientClick = "javascript:DeleteItem('" &
Convert.ToString(e.Row.RowIndex) & "')"
e.Row.Cells(GridItem.Columns.Count - 1).Controls.Add(linkb)
End If
Catch ex As Exception
lblMessage.Text = ex.Message
End Try
End Sub
Everything works fine.
But when I click a button outside the gridview, to open a window to add a
new item to the grid, the linkbuttons disappear. But the column is still
there.
If I just close the new window without saving a new data (which will
prompt the grid to rebind), the column remains empty. I had to reload the
gridview for the linkbuttons to appear.
Is it because the linkbuttons are created on rowdatabound? How should I
solve this?

No comments:

Post a Comment