【问题标题】:I need to dynamically create a control with variable and pass variable to sub我需要动态创建一个带有变量的控件并将变量传递给子
【发布时间】:2015-08-12 03:28:25
【问题描述】:

我认为这可能很容易,但我似乎没有得到它。我在 VB 工作。我在表中动态创建行,但我希望添加一个触发子的“编辑”链接/按钮/事件触发器,但我需要将变量传递给子。代码如下。请注意,我需要传递的变量是一个整数。

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

知道如何让它工作吗?

【问题讨论】:

  • 感谢您的及时回复。我会把它放在哪里?
  • lblTest.Text = (here) 行中。但是@LarsTech,Int32.Parse(DirectCast(sender, LinkButton).CommandArgument) 不是 VB.NET 中的正确方法吗?
  • @VisualVincent 是的,你的方式。我在 c# 模式下。
  • 我似乎无法让它工作。我已将其更改为: Protected Sub LinkBut​​tonEdit_OnClick(sender As Object, e As EventArgs) '很多快乐的代码,但出于示例目的,让我们使用它... lblTest.Text = Int32.Parse(DirectCast(sender, LinkBut​​ton).CommandArgument ) 结束子
  • 有什么问题?有例外吗?

标签: vb.net variables code-behind


【解决方案1】:

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

AddHandler LinkButtonEdit.OnClientClick, AddressOf LinkButtonEdit_OnClick

所以替换这个:

LinkButtonEdit.OnClientClick = "LinkButtonEdit_OnClick"

代码在顶部。


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2012-11-17
    • 2019-11-08
    • 1970-01-01
    • 2016-07-20
    相关资源
    最近更新 更多