Create linkbutton in datagrid_ItemDataBound event, how catch click?

G

geronimi

I want to create a linkbutton in a cell because not every row needs one
(so I can't setup a linkbuttoncolumn instead of a boundcolumn.)

First, i create a linkbutton in the datagrid_ItemDataBound :
Dim linkButton As New LinkButton
linkButton.Text = name
linkButton.CommandName = "PassSelectedClient"
linkButton.CommandArgument = id

I add it to the controls
e.Item.Cells(0).Controls.Add(linkButton)

Everything is done well, but I need something to catch the click!
I tried to add WithEvents but it can not be done with Dim
So i created this as a global: Protected WithEvents linkButton As New
KeepIT.WEBGUI.Controls.dcaLinkButton, but then my page is not rendered
well (only the last row contains a link :( )

How can I make it work with the click?
 
T

Teemu Keiski

Hi,

you would need to take a bit advanced approach so that control gets
recreated on postback (just ItemdataBound is not enough, since control is
not recreated without redatabinding, and that prevents postback event from
working)

Here's a post at ASP.NET Forums which should cover all you need to make it
work

Dynamically Created Controls in a Datagrid
http://forums.asp.net/745492/ShowPost.aspx

After that getting (Link)Button's Click would be either handling ItemCommand
event in the grid or wiring an event handler method to the control in code
such as

AddHandler linkButton.Click, AddressOf linkButton_Click

where linkButton_Click is a event handler method you need to write for it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top