Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 705409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:03:01+00:00 2026-05-14T04:03:01+00:00

i have a GridView (selectable) in which I want to generate a dynamic GridView

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:03:01+00:00Added an answer on May 14, 2026 at 4:03 am

    Oh my oh my, I went to MVC, anybody reading this question should do the same 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.