GridView Dynamic Buttons not firing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MikeB1
    New Member
    • Mar 2012
    • 1

    GridView Dynamic Buttons not firing

    Using SQL 2000 and VS2005
    I have a GridView control which has 12 dynamically created buttons in the header row created as follows:
    Code:
    Protected Sub gvSCTPivot_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSCTPivot.RowDataBound
            'Create the 12 buttons
            For Index = 1 To 12
                If e.Row.RowType = DataControlRowType.Header Then
                    Dim NewBtn As Button = New Button
                    NewBtn.Text = GetFinMonth(Index)
                    NewBtn.CausesValidation = False
                    NewBtn.ID = Index
                    NewBtn.Width = 35
                    NewBtn.Font.Size = 11
                    AddHandler NewBtn.Click, AddressOf btn_Clicked
                    e.Row.Cells(Index).Controls.Add(NewBtn)
    
                End If
            Next Index
    
        End Sub
    
    Sub btn_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Session("MonthNum") = CInt(CType(sender, Button).ID)
            Session("MonthName") = GetFinMonth(Session("MonthNum"))
            If Session("MonthNum") < 4 Then
                Session("Year") = Session("FinYear") - 1
            Else
                Session("Year") = Session("FinYear")
            End If
            If Me.gvSCTPivot.Rows(0).Cells(Session("MonthNum")).Text.ToString = "&nbsp;" Then
                Response.Redirect("SC+TAdd.aspx")
            Else
                Response.Redirect("SC+TEdit.aspx")
            End If
    
        End Sub
    The GridView EnableViewState property is also set to False

    One of 2 DataSource controls are bound at runtime depending on whether the underlying data exists or not. The buttons fire OK with the first DataSource control. But they do not work with the second DataSource control.

    The first DataSource control code where the buttons DO work is as follows:
    Code:
    SELECT TitleId, Name,
    Sum(Case when FinMonth = 1 then Amount else NULL end) as Apr,
    Sum(Case when FinMonth = 2 then Amount else NULL end) as May,
    Sum(Case when FinMonth = 3 then Amount else NULL end) as Jun,
    Sum(Case when FinMonth = 4 then Amount else NULL end) as Jul,
    Sum(Case when FinMonth = 5 then Amount else NULL end) as Aug,
    Sum(Case when FinMonth = 6 then Amount else NULL end) as Sep,
    Sum(Case when FinMonth = 7 then Amount else NULL end) as Oct,
    Sum(Case when FinMonth = 8 then Amount else NULL end) as Nov,
    Sum(Case when FinMonth = 9 then Amount else NULL end) as Dec,
    Sum(Case when FinMonth = 10 then Amount else NULL end) as Jan,
    Sum(Case when FinMonth = 11 then Amount else NULL end) as Feb,
    Sum(Case when FinMonth = 12 then Amount else NULL end) as Mar,
    Sum(Amount) as Total,
    Round(AVG(Amount),1) as Mean
    from SCTPivot
    left join SCTTitle
    on SCTPivot.Title = SCTTitle.TitleId
    Where FinYear = @FinYear
    Group by TitleId, Name
    Order by TitleId
    The second DataSource control code where the buttons DO NOT work is as follows:
    Code:
    Select TitleId, Name,'' as Apr, '' as May, '' as Jun, '' as Jul, '' as Aug, '' as Sep, '' as Oct, '' as Nov, '' as Dec, '' as Jan, '' as Feb, '' as Mar, '' as Total, '' as Mean
    From SCTTitle
    
    Union
    
    SELECT TitleId, Name,
    Sum(Case when FinMonth = 1 then Amount else NULL end) as Apr,
    Sum(Case when FinMonth = 2 then Amount else NULL end) as May,
    Sum(Case when FinMonth = 3 then Amount else NULL end) as Jun,
    Sum(Case when FinMonth = 4 then Amount else NULL end) as Jul,
    Sum(Case when FinMonth = 5 then Amount else NULL end) as Aug,
    Sum(Case when FinMonth = 6 then Amount else NULL end) as Sep,
    Sum(Case when FinMonth = 7 then Amount else NULL end) as Oct,
    Sum(Case when FinMonth = 8 then Amount else NULL end) as Nov,
    Sum(Case when FinMonth = 9 then Amount else NULL end) as Dec,
    Sum(Case when FinMonth = 10 then Amount else NULL end) as Jan,
    Sum(Case when FinMonth = 11 then Amount else NULL end) as Feb,
    Sum(Case when FinMonth = 12 then Amount else NULL end) as Mar,
    Sum(Amount) as Total,
    Round(AVG(Amount),1) as Mean
    from SCTPivot
    left join SCTTitle
    on SCTPivot.Title = SCTTitle.TitleId
    Where FinYear = 2010
    Group by TitleId, Name
    Order by TitleId
    Both DataSource controls display data and the buttons in the GridView. One Datasource does work with the buttons and the other does not. Please can you help?

    Thanks

    Kind Regards

    mike
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Where are you doing your DataBind in the life cycle of the page?

    Have you considered placing the buttons in the template for the GridView so that you can specify the method for the button click event instead of doing this in Code Behind?

    Have you considered configuring your GridView to use the RowCommand Event to handle the button click event?

    -Frinny

    Comment

    Working...