We help IT Professionals succeed at work.
Get Started

asp.net 4.0 linkbutton click event not running

louise001
louise001 asked
on
372 Views
Last Modified: 2012-05-09
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

Comment
Watch Question
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE