动态添加链接按钮到asp.net页面vb
我正在尝试以编程方式向页面添加一些链接按钮。
我正在尝试遵循我在网上看到的示例,但无法使其正常工作。
我希望链接按钮调用一个子组件,例如 download_file(,)
作为一个例子,我所拥有的是:
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)
让按钮出现将是一个开始!另外,我需要将它们放在面板上吗?
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该代码位于生命周期中的哪个事件中?我相信,您必须在 Init 或 PreInit 页面事件中动态添加控件。
What event in the lifecycle is that code in? You have to add controls dynamically, I believe, in the Init or PreInit page events.
这可能是回发问题。您可能想尝试设置一个断点,看看这段代码是否被调用。我在按钮控件的单击事件中测试了您的代码(当您单击它时会自动回发),并且它确实有效。但是,我确实取出了 reader() 部分,并将“Panel1”替换为“Form”(这应该没有什么区别)。
另外,如果您要动态添加面板,请确保将其也添加到页面的控件中。
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.
将代码写在init方法中。
write the code inside init method.