【发布时间】: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" /> 
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Width="300px" />
【问题讨论】:
-
cNumber肯定是 1 吗?这些控件是否以某种方式嵌套?如果是这样,请查看this question -
No cNumber 不定义为 1。它的范围可以从 1 到 20。它由调用按钮定义,即 ImageButton#,其中 # 可以从 1 到 20。不,它们不是嵌套的。每组文本框和图像按钮都在 asp.net 表的一行中。