2

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?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

hi, yeah I moved the code to the init pag event and the buttons now appear. Enough to get me started on them! thanks.
0

It could be a postback issue. You might want to try setting a break point and seeing if this code ever gets called. I tested your code inside the click event of a button control (which automatically posts back when you click it), and it did work. However, I did take out the reader() part and I replaced "Panel1" with "Form" (this shouldn't make a difference).

Also, if you're adding your panel dynamically, make sure you add it to the page's controls as well.

Comments

0

write the code inside init method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.