从动态生成的LinkBut​​ton中获取ID

时间:2014-03-10 15:40:53

标签: vb.net visual-studio-2012 .net-4.0

我在后面的代码中动态生成按钮,并从数据库表中为其分配ID。

Dim subjectLinkButton As LinkButton = New LinkButton()
subjectLinkButton.Attributes.Add("ID", doc.InboxId)
subjectLinkButton.Text = doc.ReceivedSubject
AddHandler subjectLinkButton.Click, AddressOf subjectLinkButton_Click

当我点击按钮时,我用这个

Dim btn As LinkButton = DirectCast(sender, LinkButton)
Me.debugTest.InnerText = "Button ID:" & btn.ID

我收到此错误:     你调用的对象是空的。在Inbox.subjectLinkBut​​ton_Click(Object sender,EventArgs e)

1 个答案:

答案 0 :(得分:1)

将Attributes.Add更改为

        subjectLinkButton.CommandName = "inbox_id"
        subjectLinkButton.CommandArgument = doc.InboxId

        Dim btn As LinkButton = DirectCast(sender, LinkButton)
        Me.debugTest.InnerText = "Button: " & btn.CommandName & " " & btn.CommandArgument