Dynamic gridview columns event problem

Posted by ropstah on Stack Overflow See other posts from Stack Overflow or by ropstah
Published on 2009-03-31T12:44:05Z Indexed on 2010/03/29 1:03 UTC
Read the original article Hit count: 596

Filed under:
|
|
|

Hi,

i have a GridView (selectable) in which I want to generate a dynamic GridView in a new row BELOW the selected row.

  1. I can add the row and gridview dynamically in the Gridview1 PreRender event. I need to use this event because:
    • _OnDataBound is not called on every postback (same for _OnRowDataBound)
    • _OnInit is not possible because the 'Inner table' for the Gridview is added after Init
    • _OnLoad is not possible because the 'selected' row is not selected yet.
  2. I can add the columns to the dynamic GridView based on my ITemplate class. But now the button events won't fire.... Any suggestions?

The dynamic adding of the gridview:

Private Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender
    Dim g As GridView = sender
    g.DataBind()

    If g.SelectedRow IsNot Nothing AndAlso g.Controls.Count > 0 Then
        Dim t As Table = g.Controls(0)
        Dim r As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
        Dim c As New TableCell

        Dim visibleColumnCount As Integer = 0

        For Each d As DataControlField In g.Columns
            If d.Visible Then
                visibleColumnCount += 1
            End If
        Next

        c.ColumnSpan = visibleColumnCount
        Dim ph As New PlaceHolder

        ph.Controls.Add(CreateStockGrid(g.SelectedDataKey.Value))

        c.Controls.Add(ph)

        r.Cells.Add(c)
        t.Rows.AddAt(g.SelectedRow.RowIndex + 2, r)
    End If
End Sub

Private Function CreateStockGrid(ByVal PnmAutoKey As String) As GridView

    Dim col As Interfaces.esColumnMetadata
    Dim coll As New BLL.ViewStmCollection
    Dim entity As New BLL.ViewStm
    Dim query As BLL.ViewStmQuery = coll.Query

    Me._gridStock.AutoGenerateColumns = False

    Dim buttonf As New TemplateField()
    buttonf.ItemTemplate = New QuantityTemplateField(ListItemType.Item, "", "Button")
    buttonf.HeaderTemplate = New QuantityTemplateField(ListItemType.Header, "", "Button")
    buttonf.EditItemTemplate = New QuantityTemplateField(ListItemType.EditItem, "", "Button")

    Me._gridStock.Columns.Add(buttonf)

    For Each col In coll.es.Meta.Columns
        Dim headerf As New QuantityTemplateField(ListItemType.Header, col.PropertyName, col.Type.Name)
        Dim itemf As New QuantityTemplateField(ListItemType.Item, col.PropertyName, col.Type.Name)
        Dim editf As New QuantityTemplateField(ListItemType.EditItem, col.PropertyName, col.Type.Name)

        Dim f As New TemplateField()

        f.HeaderTemplate = headerf
        f.ItemTemplate = itemf
        f.EditItemTemplate = editf

        Me._gridStock.Columns.Add(f)
    Next

    query.Where(query.PnmAutoKey.Equal(PnmAutoKey))

    coll.LoadAll()

    Me._gridStock.ID = "gvChild"
    Me._gridStock.DataSource = coll
    AddHandler Me._gridStock.RowCommand, AddressOf Me.gv_RowCommand

    Me._gridStock.DataBind()

    Return Me._gridStock
End Function

The ITemplate class:

Public Class QuantityTemplateField : Implements ITemplate
Private _itemType As ListItemType
Private _fieldName As String
Private _infoType As String

Public Sub New(ByVal ItemType As ListItemType, ByVal FieldName As String, ByVal InfoType As String)
    Me._itemType = ItemType
    Me._fieldName = FieldName
    Me._infoType = InfoType
End Sub

Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
    Select Case Me._itemType
        Case ListItemType.Header
            Dim l As New Literal
            l.Text = "&lt;b&gt;" & Me._fieldName & "</b>"
            container.Controls.Add(l)
        Case ListItemType.Item
            Select Case Me._infoType
                Case "Button"
                    Dim ib As New Button()
                    Dim eb As New Button()
                    ib.ID = "InsertButton"
                    eb.ID = "EditButton"
                    ib.Text = "Insert"
                    eb.Text = "Edit"
                    ib.CommandName = "Edit"
                    eb.CommandName = "Edit"
                    AddHandler ib.Click, AddressOf Me.InsertButton_OnClick
                    AddHandler eb.Click, AddressOf Me.EditButton_OnClick
                    container.Controls.Add(ib)
                    container.Controls.Add(eb)
                Case Else
                    Dim l As New Label
                    l.ID = Me._fieldName
                    l.Text = ""
                    AddHandler l.DataBinding, AddressOf Me.OnDataBinding
                    container.Controls.Add(l)
            End Select
        Case ListItemType.EditItem
            Select Case Me._infoType
                Case "Button"
                    Dim b As New Button
                    b.ID = "UpdateButton"
                    b.Text = "Update"
                    b.CommandName = "Update"
                    b.OnClientClick = "return confirm('Sure?')"
                    container.Controls.Add(b)
                Case Else
                    Dim t As New TextBox
                    t.ID = Me._fieldName
                    AddHandler t.DataBinding, AddressOf Me.OnDataBinding

                    container.Controls.Add(t)
            End Select
    End Select
End Sub

Private Sub InsertButton_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    Console.WriteLine("insert click")
End Sub

Private Sub EditButton_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    Console.WriteLine("edit click")
End Sub

Private Sub OnDataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim boundValue As Object = Nothing
    Dim ctrl As Control = sender
    Dim dataItemContainer As IDataItemContainer = ctrl.NamingContainer
    boundValue = DataBinder.Eval(dataItemContainer.DataItem, Me._fieldName)

    Select Case Me._itemType
        Case ListItemType.Item
            Dim fieldLiteral As Label = sender
            fieldLiteral.Text = boundValue.ToString()
        Case ListItemType.EditItem
            Dim fieldTextbox As TextBox = sender
            fieldTextbox.Text = boundValue.ToString()
    End Select
End Sub
End Class

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about gridview