Dynamically Created Gridview with Button in footer template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joss Perold
    New Member
    • Feb 2011
    • 14

    Dynamically Created Gridview with Button in footer template

    Hi

    I have created a gridview dynamically from scratch and added it to my aspx page. This works fine, but i have a button in the footer of on of the columns, which when clicked on makes the gridview disappear and doesn't fire the onclick event that it should. This should obviously not be the case. My question is why does the event not fire and why does the gridview vanish?

    Bear in mind i have never created aa gridview in the code behind before, so excuse me if i am being dense

    Code creating columns
    Code:
       Dim gvTownships As New GridView
                'COLUMNS
                Dim tmpCategory As New TemplateField
                tmpCategory.ItemTemplate = New cGridViewTemplate(DataControlRowType.DataRow, "Category")
                tmpCategory.HeaderTemplate = New cGridViewTemplate(DataControlRowType.Header, "Category")
                tmpCategory.FooterTemplate = New cGridViewTemplate(DataControlRowType.Footer, "Calculation")
       'INFORMATION
                gvTownships.AutoGenerateColumns = False
                gvTownships.ID = cmbChoice.SelectedItem.Text
                gvTownships.ShowFooter = True
                gvTownships.CssClass = "mGrid"
                gvTownships.RowStyle.Wrap = False
                gvTownships.HeaderStyle.CssClass = "mgrid"
                gvTownships.HeaderStyle.Wrap = True
                gvTownships.FooterStyle.CssClass = "mGridtf"
                gvTownships.AlternatingRowStyle.CssClass = "alt"
                gvTownships.GridLines = GridLines.Both
    
                'add columns
                gvTownships.Columns.Add(tmpCategory)
    
      gvTownships.DataSource = dstSuburbTowhShip
                    gvTownships.DataBind()
    Code to add button to footer

    Code:
    Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn
    
                    Case DataControlRowType.Footer
                        If columnName = "Calculation" Then
                            Dim btnCalculate As New Button
                            btnCalculate.Text = "Calculate"
                            btnCalculate.CssClass = "button"
    
                            'add handler
                            AddHandler btnCalculate.Click, AddressOf btnCalculate_Clicked
                            container.Controls.Add(btnCalculate)
    
                        End If
    
                    Case Else
    
                        ' Insert code to handle unexpected values. 
    
                End Select
    
            End Sub
         Private Sub btnCalculate_Clicked(ByVal sender As Object, ByVal e As EventArgs)
                Try
                    Dim btnCalculate As Button = CType(sender, Button)
                    Dim gvRow As GridViewRow = CType(btnCalculate.NamingContainer, GridViewRow)
                    btnCalculate.CommandArgument = "Calculate"
                Catch ex As Exception
    
                End Try
            End Sub
        End Class
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    When you clic the button below, the page loads again. if you dont generate the gv again, there is no reason for it to be shown.

    Comment

    • joss Perold
      New Member
      • Feb 2011
      • 14

      #3
      Thanks for the prompt reply, so as i understand it the grid has to be rebuilt, thats fine, but how do i do that if the onclick event doesnt fire?

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Mmm, perhaps the problem is because the page loads first, and then try to runs an event that isn't handled anymore... I'm not sure, but that could be he problem. Working with code created controls always give trouble in aspx.

        Have you tried to create it in your project, and fill it with your info at runtime? just put it into a div that runs at server, with its display set to 'none' and set it to 'block' when you want to see it (or with its visible attribute).

        Comment

        • Rob S
          New Member
          • Jan 2011
          • 14

          #5
          Have you found an answer to this problem yet?

          Comment

          • Marknut
            New Member
            • Apr 2010
            • 42

            #6
            I don’t understand what the footer button, “Calculate ,” does. Does it calculate the contents of the gridivew? If so, where does the calculation go?

            Without the answers to those questions, the closest fix I can come up with is to use JavaScript for the “onClick” event of the button and finish the javascript code with “ return false;” so that there’s no postback.

            Comment

            Working...