Проблема события динамического столбца сетки - PullRequest
0 голосов
/ 31 марта 2009

У меня есть GridView (по выбору), в котором я хочу создать динамический GridView в новой строке ниже выбранной строки.

  1. Я могу динамически добавлять строку и вид сетки в событие Gridview1 PreRender. Мне нужно использовать это событие, потому что:
    • _OnDataBound не вызывается при каждой обратной передаче (то же самое для _OnRowDataBound)
    • _OnInit невозможно, потому что «Внутренняя таблица» для Gridview добавляется после Init
    • _OnLoad невозможно, потому что выбранная строка еще не выбрана.
  2. Я могу добавить столбцы в динамический GridView на основе моего класса ITemplate. Но теперь события кнопки не сработают ... Есть предложения?

Динамическое добавление вида сетки:

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  

Класс ITemplate:

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 Ответ

2 голосов
/ 29 марта 2010

О, боже мой, я пошел в MVC, любой, кто читает этот вопрос, должен сделать то же самое:)

...