Link to home
Start Free TrialLog in
Avatar of louise001
louise001

asked on

asp.net 4.0 linkbutton click event not running

Hi,
My code behind page in an asp.net 4.0 application builds an asp table at runtime. The final cell in each row is a linkbutton (LinkButtonEdit) with a click event which adds a text box to each cell in the row. It then adds a cell containing another link button (LinkButtonSave), and an event handler for LinkButtonSave.Click. However the click event for LinkButtonSave.Click doesn't run. When I click LinkButtonEdit, LinkButtonSave appears as intended but then when I click LinkButtonSave it and its containing cell just disappears.

I can see that I'm calling AddHandler for LinkButtonSave.Click within an event called by an earlier AddHandler (LinkButton.Edit's Add Handler) but I don't know if that's why it's not working. I've added my code for clarity. Can anyone help with this?

Thanks in advance,

Louise
'add the link button to the table
Dim LinkButtonEdit As New LinkButton
                        LinkButtonEdit.ID = "LinkButtonEdit" + row_data.ID.ToString
                        LinkButtonEdit.Text = "Edit"
                        cell.Controls.Add(LinkButtonEdit)
                        'Add handler
                        AddHandler LinkButtonEdit.Click, AddressOf EditRevenues

'This is my sub EditRevenues:
Dim lnk As LinkButton
            lnk = CType(sender, LinkButton)

            Dim EmpID As Integer = CInt(lnk.ID.Replace("LinkButtonEdit", "")) 'this gives the link button's emp id
            Dim MonthID As Integer

            'Add text boxes to the row
            For m As Integer = 1 To 12
                For i As Integer = 2 To tEmps.Rows.Count + 1
                    If t.Rows(i).ID = EmpID Then
                        MonthID = CInt(t.Rows(1).Cells(m).ID.Replace("MonthID", ""))
                        Dim TextBoxEdit As New TextBox
                        TextBoxEdit.ID = MonthID.ToString
                        TextBoxEdit.Width = 30
                        TextBoxEdit.BackColor = Drawing.Color.LightGray
                        TextBoxEdit.Text = t.Rows(i).Cells(m).Text.ToString
                        t.Rows(i).Cells(m).Controls.Add(TextBoxEdit)
                    End If
                Next
            Next

            'Add save button to the row
            For Int As Integer = 2 To tEmps.Rows.Count + 1
                If t.Rows(Int).ID = EmpID Then
                    Dim cell_save As New TableCell
                    Dim LinkButtonSave As New LinkButton
                    LinkButtonSave.ID = "LinkButtonSave" + EmpID.ToString
                    LinkButtonSave.Text = "Save"
                    cell_save.Controls.Add(LinkButtonSave)
                    t.Rows(Int).Cells.Add(cell_save)
                    'Add handler
                    AddHandler LinkButtonSave.Click, AddressOf SaveRevenues
                End If
            Next

'This is my sub SaveRevenues:
'implementation here but SaveRevenues doesn't execute

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nmarun
nmarun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of louise001
louise001

ASKER

Hi,
Thanks for your reply, having read the article I've got it working. I'm now instantiating LinkButtonSave in the sub which builds the table, rather than recursively:
 If cellNum = numCells - 1 Then
                        Dim LinkButtonSave As New LinkButton
                        LinkButtonSave.ID = "LinkButtonSave" + row_data.ID.ToString
                        LinkButtonSave.Text = "Save"
                        cell.Controls.Add(LinkButtonSave)
                        cell.Visible = False
                        'Add handler
                        AddHandler LinkButtonSave.Click, AddressOf SaveRevenues
                    End If
'code to build table at runtime continues here

Will award points now.

Thanks again,

Louise