Results 1 to 3 of 3

Thread: Persisting RunTime created controls

  1. #1

    Thread Starter
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Persisting RunTime created controls

    I am using a DataGrid. In it, I add a cell and then attempt to add an evenhandler.

    In any case, whenever anything is clicked on the page, the added cells drop from the datagrid -- EVEN IF I do not do anything with the datagrid.

    In the past, usually datagrids don't change unless you change them.

    Anyone run in to this? How can I make the DataGrid persist the added cells?

    My Code:

    Code:
        Public Sub QueueDataGrid_Bound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles QueueDataGrid.ItemDataBound
            Dim tempVal As String
            Dim DItem As Object 'System.Web.UI.WebControls.DataGridItem
            Dim intI As Integer
            Dim TTCell As New System.Web.UI.WebControls.TableCell()
            Static CellsList() As Boolean
            If e.Item.ItemType = System.Web.UI.WebControls.ListItemType.Header Then
                ReDim CellsList(e.Item.Cells.Count)
                For intI = 0 To e.Item.Cells.Count - 1
                    If Not IsDBNull(e.Item.Cells(0).ToString) Then
                        If UCase(Left((e.Item.Cells(intI).Text.ToString()), 3)) = "HDN" Then
                            CellsList(intI) = False
                            e.Item.Cells(intI).Visible = False
                        Else
                            CellsList(intI) = True
                            e.Item.Cells(intI).Visible = True
                        End If
                    End If
                    '   Response.Write(e.Item.DataItem.item(0).ToString())
                Next
                TTCell.Text = ""
                e.Item.Cells.AddAt(e.Item.Cells.Count, TTCell)
            End If
    
            If e.Item.ItemType = System.Web.UI.WebControls.ListItemType.Item Or e.Item.ItemType = System.Web.UI.WebControls.ListItemType.AlternatingItem Or e.Item.ItemType = System.Web.UI.WebControls.ListItemType.Footer Then
                For intI = 0 To e.Item.Cells.Count - 1
                    e.Item.Cells(intI).Visible = CellsList(intI)
                Next
    
                Dim ViewCell As New System.Web.UI.HtmlControls.HtmlButton()
                If Not IsDBNull(e.Item.Cells(0).ToString()) Then
                    ViewCell.InnerText = "View"
                    ViewCell.Attributes.Add("onClick", "javascript:window.open('default.aspx?vwRec=" & e.Item.Cells(0).Text.ToString() & "');void[];")
                    ViewCell.Attributes.Add("Class", "QueueButton")
                    TTCell.Controls.Add(ViewCell)
    
                    TTCell.Controls.Add(New LiteralControl("|"))
    
                    Dim TakeCell As New System.Web.UI.WebControls.Button()
                    TakeCell.Text = "Take"
                    'TakeCell.Attributes.Add("Class", "QueueButton")
                    TakeCell.ID = e.Item.Cells(0).Text.ToString()
                    AddHandler TakeCell.Click, AddressOf TakeCellClick
    
                    e.Item.Cells.AddAt(e.Item.Cells.Count, TTCell)
                    e.Item.Cells(e.Item.Cells.Count - 1).Controls.Add(TakeCell)
    
                End If
            End If
        End Sub
    
        Public Sub TakeCellClick(ByVal sender As Object, ByVal e As System.EventArgs) 'ByVal e As ImageClickEventArgs)  Handles bn.Click
            LoginApp.Visible = True
            Response.Write("I was clicked")
        End Sub
    I never get "I was clicked" outputted to the page and the extra table cell column drops from the table
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  2. #2
    Lively Member
    Join Date
    Mar 2002
    Posts
    96
    you need to check for postback in page_load function

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also try declaring the runtime variables in a module instead of the page. I don't know for sure about eventhandlers but with delegates you have to reset them at every postback.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width