Help with "sort of working" adding a link button to a datagrid dynamically

B

Burak Gunay

Hello,

I am trying to write the dynamic code behind version of this code in
the data behind:

<asp:TemplateColumn HeaderStyle-ForeColor="white" Headertext="ID">
<ItemTemplate>
<asp:LinkButton ID="lnkCopyPnl" CausesValidation=False
OnCommand="CopyLinkButton_Command" CommandName='Id'
CommandArgument='<%# Container.DataItem("id") %>' Runat=server>
<%# Container.DataItem("id") %>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

what I have below sort of works,

Class myLinkButtonColumn
Implements ITemplate
Sub instantiatein(ByVal container As Control) Implements
ITemplate.InstantiateIn
Dim btn As New LinkButton
btn.ID = "lnkData"
btn.CausesValidation = False
btn.CommandName = "id"
AddHandler btn.DataBinding, AddressOf Me.BindButtonColumn
AddHandler btn.Command, AddressOf Me.LinkButton_Command
container.Controls.Add(btn)
End Sub

Sub BindButtonColumn(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As LinkButton = CType(sender, LinkButton)
Dim container As DataGridItem = CType(btn.NamingContainer,
DataGridItem)
btn.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, "id"))
btn.CommandArgument =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, "id"))
End Sub

Sub LinkButton_Command(ByVal sender As Object, ByVal e As
CommandEventArgs)
MsgBox("hello world")
End Sub

End Class

the id values are showing up and they are clickable, but the problem is
when a value is clicked, the LinkButton_Command does not get called and
the datagrid disappears to boot!

Any help will be appreciated.

Regards,

Burak
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top