Search code examples
asp.netlinkbutton

dynamically add linkbutton to asp.net page vb


I'm trying to programatically add some link buttons to a page.

I'm trying to follow an example that I've seen online but can't get it to work.

I want the linkbutton to call a sub e.g. download_file(,)

As an example what I have is:

            Dim lb = New LinkButton()
            lb.CausesValidation = True
            lb.Attributes.Add("runat", "server")
            lb.CommandName = "lb_Click"
            lb.CommandArgument = "test"
            lb.Text = reader("filename")
            lb.EnableViewState = True
            lb.Enabled = True

            AddHandler lb.Click, AddressOf download_file

            Panel1.Controls.Add(lb)

Getting the button(s) to appear would be a start! Also, do I need to put them on a panel?

Any ideas?


Solution

  • What event in the lifecycle is that code in? You have to add controls dynamically, I believe, in the Init or PreInit page events.