我需要动态创建一个带有变量的控件并将变量传递给 sub

I need to dynamically create a control with variable and pass variable to sub

我认为这可能很容易,但我似乎不明白。我在 VB 工作。我在 table 中动态创建行,但我希望添加一个 "EDIT" link/button/event-trigger-of-some-kind 来触发子程序,但我需要将变量传递给子程序。代码如下。请注意,我需要传递的变量是一个整数。

Dim CellDescriptionEdit As New TableCell()
RowNameDescription.Cells.Add(CellDescriptionEdit)
CellDescriptionEdit.ID = "CellDescriptionEdit" & UserReader("fldClaimPmtID")
Dim LinkButtonEdit As New LinkButton
LinkButtonEdit.CommandArgument = UserReader("fldClaimPmtID")
LinkButtonEdit.OnClientClick = "LinkButtonEdit_OnClick"
LinkButtonEdit.Text = "EDIT"
CellDescriptionEdit.Controls.Add(LinkButtonEdit)

然后潜艇看起来像这样...

Protected Sub LinkButtonEdit_OnClick(sender As Object, e As EventArgs)
   'Lots of happy code but for example purposes let's use this...        
    lblTest.Text = Int32.Parse(btn.CommandArgument)
End Sub

知道如何让它工作吗?

要将事件处理程序添加到您使用 AddHandler 语句的控件:

AddHandler LinkButtonEdit.OnClientClick, AddressOf LinkButtonEdit_OnClick

所以替换这个:

LinkButtonEdit.OnClientClick = "LinkButtonEdit_OnClick"

代码在顶部。


如果您使用 ASP.NET,这也是需要考虑的内容:Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event