【问题标题】:Reference specific asp.net control using vb.net code bhind variable使用 vb.net 代码隐藏变量引用特定的 asp.net 控件
【发布时间】:2017-08-18 23:57:17
【问题描述】:

我已经在 SO 和其他网站上阅读了许多不同的解决方案,但这一直让我感到困惑。

我正在尝试从 vb.net 后面的代码中引用一个 asp.net 文本框和图像按钮。

我试过这段代码,但变量 tBox 说它等于什么都没有。

Dim callBtn As ImageButton = CType(sender, ImageButton)
Dim tBox As TextBox
Dim iButton As ImageButton
Dim cNumber As Integer = Convert.ToInt32(callBtn.ID.Substring(11))
tBox = CType(Page.FindControl("TextBox" & cNumber), TextBox)
'The line below is commented out because it did not work either but
was the first one I tried
'tBox = DirectCast(Page.FindControl("TextBox" & cNumber), TextBox)
tBox.Text = "" <--- On this line tBox is nothing
tBox.Visible = False

我在 tBox.Text 行放置了一个断点,而 tBox 不等于任何内容。

如何从变量中引用文本框?我还需要引用和图像按钮,但需要先让这个按钮工作。

如果这里有一个问题可以准确回答这个问题,请指点我,因为我找不到。

编辑:更新 这是创建文本框和图像按钮的代码。

<asp:ImageButton ID="ImageButton1" runat="server" Visible="false" OnClick="removeFile" ImageUrl="~/Images/red-x-md20x20.png" ImageAlign="Top" ToolTip="Click To Remove File" />&ensp;
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Width="300px" />

【问题讨论】:

  • cNumber 肯定是 1 吗?这些控件是否以某种方式嵌套?如果是这样,请查看this question
  • No cNumber 不定义为 1。它的范围可以从 1 到 20。它由调用按钮定义,即 ImageButton#,其中 # 可以从 1 到 20。不,它们不是嵌套的。每组文本框和图像按钮都在 asp.net 表的一行中。

标签: asp.net vb.net


【解决方案1】:

事实证明,我不是在寻找正确的名称。在内容页面上,我必须先参考母版页。

我使用了以下内容:

Dim ctrlNameT As String = "ctl00$ContentPlaceHolderRight$TextBox" & cNumber

这成功了!

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2011-11-02
    • 2010-09-07
    • 1970-01-01
    • 2012-01-06
    • 2010-10-03
    相关资源
    最近更新 更多