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?
What event in the lifecycle is that code in? You have to add controls dynamically, I believe, in the Init or PreInit page events.
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.
write the code inside init method.
精彩评论