Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling LinkButton event of dynamicaly generated table?

I want to handle click event of link button which is dynamically created.

But i am not getting btn.click for it.

I have following code:

Public Sub test()
        Dim row As New HtmlTableRow()
        Dim cell As New HtmlTableCell()


        For i = 0 To 10
            row = New HtmlTableRow()
            For j = 0 To 3
                cell = New HtmlTableCell()
                cell.InnerText = "m"
                Dim btn1 As New LinkButton
                btn1.ID = i

                cell.Controls.Add(btn1)
                row.Cells.Add(cell)
            Next
            tableContent.Rows.Add(row)
        Next
    End Sub

C# Code:

public void test()
{
    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = new HtmlTableCell();


    for (i = 0; i <= 10; i++) {
        row = new HtmlTableRow();
        for (j = 0; j <= 3; j++) {
            cell = new HtmlTableCell();
            cell.InnerText = "m";
            LinkButton btn1 = new LinkButton();
            btn1.ID = i;

            cell.Controls.Add(btn1);
            row.Cells.Add(cell);
        }
        tableContent.Rows.Add(row);
    }
}

Not getting intellisence on btn1.click:

enter image description here

EDIT:

enter image description here

Answer in c# can also help me.

like image 986
C Sharper Avatar asked Feb 03 '26 07:02

C Sharper


2 Answers

For VB.NET, type AddHandler btn1. and then you should see the Click event in the Intellisense, like this:

AddHandler btn1.Click, AddressOf Me.LinkButton_OnClick

Since you have dynamic content (table), you will need to rebuild the table on every page load, not just the first. The reason for this is that the Page_Load happens before the link button click event, so by the time the click event happens the table needs to have been recreated; otherwise your click event handler will try to interact with content that is not there.


Read ASP.NET Page Life Cycle Overview for more information about the page life cycle and all the events and their order.

like image 95
Karl Anderson Avatar answered Feb 04 '26 23:02

Karl Anderson


In C# you can do:
1. Declare the handler

protected void btn1_Click(object sender, EventArgs e)
{

}

2. Assign the handler:

LinkButton btn1 = new LinkButton();
btn1.Click += new EventHandler(btn1_Click);
like image 33
Ganesh Jadhav Avatar answered Feb 04 '26 23:02

Ganesh Jadhav