GridView.Footer - LinkButton problem

Discussion in 'C#' started by snufse, May 21, 2009.

  1. #1
    I have an application where I need to assign a link button to the footer. For some reason, when I click on the link button (in the footer) nothing happens. I've been working on this for days and need good help please:



    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound 
    Dim tabledata As Data.DataRowView = e.Row.DataItem
    
    If e.Row.RowType = DataControlRowType.Footer Then
    
    myLinkButton.Text = "Select"
    
    myLinkButton.CommandName = "SelectFooter"
    
    AddHandler myLinkButton.Click, New EventHandler(AddressOf ClickHandler) 
    e.Row.Cells(0).Controls.Add(myLinkButton)
    
    e.Row.Cells(2).Text = "Selection Total"
    
    e.Row.Cells(3).Text = Session("TotalHours") 
    End If
    
    End Sub
    Code (markup):


    Public Sub ClickHandler(ByVal sender As Object, ByVal e As EventArgs) 
    
    'Handle LinkButton click event here. But this event never gets called...
    
    End Sub
    Code (markup):

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    
            If e.CommandName = "Select" Then
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Dim mySelectedRow As GridViewRow = GridView1.Rows(index)
                Dim mySelectedCell As TableCell
    
                mySelectedCell = mySelectedRow.Cells(1)
                Equipment_ID = mySelectedCell.Text.ToString
    
                GetItemDetail(Equipment_ID)
                DisplayItemDetail()
            End If
    
            If e.CommandName = "SelectFooter" Then
                ClickHandler(sender, e)
            End If
    
        End Sub
    Code (markup):
     
    snufse, May 21, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I don't see you declare the button:
    Dim myLinkButton As New LinkButton

    I can only assume that you declare it globally. Try declaring it within the GridView1_RowDataBound() sub. I don't know whether this will make a difference but otherwise i can't see any other problem.

    -EDIT-
    actually i don't think it's gonna make a difference.
     
    camjohnson95, May 22, 2009 IP
    snufse likes this.
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    This works for me:
    
        Public Sub ClickHandler(ByVal sender As Object, ByVal e As EventArgs)
            MsgBox("test")
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim myLinkButton As New LinkButton
            myLinkButton.Text = "Select"
            AddHandler myLinkButton.Click, New EventHandler(AddressOf ClickHandler)
            GridView1.FooterRow.Cells(0).Controls.Add(myLinkButton)
        End Sub
    
    Code (markup):
     
    camjohnson95, May 22, 2009 IP
  4. snufse

    snufse Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi camjohnson95,

    I very much appreciate you helping me out. I've been working on this problem for days without getting anywhere. This is now what I have in the RowDataBound (added the dim for LinkButton). When I run the app the footer shows up and there is a "Select" that I can click in cell(0). When I click it, nothing happens (does not enter the ClickHandler event) and the "Select" text disappears (the cell goes blank). Do I need to do something in the mark up section (I would not think so but I am not very familiar with .Net development). Instead of using a handler could I use a selectcommand? Just a thought. Again, I am very grateful for any help....


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            Dim tabledata As Data.DataRowView = e.Row.DataItem
            Dim myLinkButton As New LinkButton
    
            If e.Row.RowType = DataControlRowType.Footer Then
                myLinkButton.Text = "Select"
                AddHandler myLinkButton.Click, New EventHandler(AddressOf ClickHandler)
                myLinkButton.CommandName = "SelectFooter"
                e.Row.Cells(0).Controls.Add(myLinkButton)
                e.Row.Cells(2).Text = "Selection Total"
                e.Row.Cells(3).Text = Session("TotalHours")
            End If
    
        End Sub
    Code (markup):

    Public Sub ClickHandler(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim mystr As String
            mystr = "Hello"
            'Handle LinkButton click event here. But this event never gets called...
    
     End Sub
    Code (markup):
    I have included the mark up code just in case.


    <asp:GridView ID="GridView1" runat="server" Style="z-index: 102; left: 0px; position: relative;
                            top: 43px; border-right: gray thin solid; border-top: gray thin solid; border-left: gray thin solid;
                            border-bottom: gray thin solid; font-size: 12px; overflow: hidden; clip: rect(auto auto auto auto);"
                            Width="342px" AllowPaging="True" AllowSorting="True" Font-Size="Small" AutoGenerateColumns="False"
                            CellSpacing="2" HorizontalAlign="Left" BorderColor="LightGray" BorderStyle="None"
                            PageSize="14" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand"
                            EmptyDataText="No records found" ShowFooter="True">
                            <Columns>
                                <asp:CommandField HeaderText="Detail" ShowSelectButton="True">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" Width="10px" />
                                </asp:CommandField>
                                <asp:BoundField DataField="equipmentID" HeaderText="Equipment ID" SortExpression="equipmentID">
                                    <ItemStyle HorizontalAlign="Left" Width="60px" />
                                    <HeaderStyle HorizontalAlign="Center" />
                                </asp:BoundField>
                                <asp:BoundField DataField="equipmentname" HeaderText="Equipment Name">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Left" Width="240px" />
                                </asp:BoundField>
                                <asp:BoundField DataField="hours" HeaderText="Hours" NullDisplayText="0">
                                    <ItemStyle HorizontalAlign="Right" Width="50px" />
                                    <FooterStyle HorizontalAlign="Right" />
                                </asp:BoundField>
                            </Columns>
                            <HeaderStyle BackColor="#FFC080" Height="35px" />
                            <AlternatingRowStyle BackColor="#FFFFC0" />
                            <SelectedRowStyle BackColor="#FF8080" />
                            <EditRowStyle BackColor="#FF8080" />
                            <RowStyle Height="20px" />
                            <FooterStyle BackColor="#FF8080" />
                        </asp:GridView>
    Code (markup):
     
    snufse, May 22, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    The same thing happened for me, when I created a test project in visual studio. I found that if you add the button within the Page_load event and not the Gridview_RowDataBound event, and just use the GridView1.FooterRow reference then it works fine, as i showed in the example in my second post. Is there a problem with using this method? Another solution is to simple add the linkbutton to the footer within the markup.
     
    camjohnson95, May 22, 2009 IP
  6. snufse

    snufse Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi camjohnson95,

    Thank you I will let you know. Thanks so much.
     
    snufse, May 22, 2009 IP
  7. snufse

    snufse Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hi camjohnsosn95,

    I tried what you said and have following.


    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim myLinkButton As New LinkButton
            myLinkButton.Text = "Select"
            AddHandler myLinkButton.Click, New EventHandler(AddressOf ClickHandler)
            GridView1.FooterRow.Cells(0).Controls.Add(myLinkButton)
           
    End Sub
    Code (markup):

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            Dim tabledata As Data.DataRowView = e.Row.DataItem
           
            If e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Cells(2).Text = "Selection Total"
                e.Row.Cells(3).Text = Session("TotalHours")
            End If
    
    End Sub
    Code (markup):
    Now when I run I get error on"GridView1.FooterRow.Cells(0).Controls.Add(myLinkButton)" -> Object reference not set to an instance of an object. I read that "The FooterRow property is available only after the GridView control creates the footer row in the RowCreated event" (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.footerrow(VS.80).aspx)

    Any clue what I am doing wrong? Thank you.
     
    snufse, May 22, 2009 IP
  8. snufse

    snufse Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi camjohnson95,

    Just about to pull my hair out when I discovered following:

    If I put the "footer" code in the GridView1_RowCreated it works. When I click the "select" link button in the footer row, it fires the ClickHandler event.

    I guess this means I should be all set, I definitely learned something. Thank you again for all your help.


    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated. 
    
    If e.Row.RowType = DataControlRowType.Header Then
       
            If e.Row.RowType = DataControlRowType.Footer Then
                Dim myLinkButton As New LinkButton
                myLinkButton.Text = "Select"
                AddHandler myLinkButton.Click, New EventHandler(AddressOf ClickHandler)
                e.Row.Cells(0).Controls.Add(myLinkButton)
                e.Row.Cells(2).Text = "Selection Total"
                e.Row.Cells(3).Text = Session("TotalHours")
    End If
    
    End Sub
    Code (markup):
     
    snufse, May 22, 2009 IP