【发布时间】:2023-04-10 07:40:01
【问题描述】:
这很奇怪,我现在已经摸不着头脑了好几个小时。我有一个带有文本框(HTMLInputText)的转发器,这个文本框将根据数据显示或隐藏。我有这样的场景,第一个转发器项目将显示此文本框,而下一个转发器项目将隐藏此文本框。我有一个方法可以循环遍历转发器以获取数据,如果找到它,我正在检查这个文本框,是否只是为了指示每个转发器项目的状态。问题是,在第二个转发器项目上,由于它是隐藏的(显示=无),因此不应找到文本框,我仍在找到它,我不希望得到任何价值。检查 html 标记,没有按预期找到隐藏的文本框。为什么哦为什么我什么也没得到?返回的是 {value="0"} 这实际上是文本框的初始值。请不要告诉我将我的文本框更改为 asp:textbox。
For each rptItem As RepeaterItem in repeater.Items
dim tbx As HtmlInputText = rptItem.FindControl("tbxPrice")
If tbx isNot Nothing Then
'process here
Else
'another process
End If
Next
我实际上得到了转发器中其他项目的正确数据。
编辑:附加代码 HTML:
<div id="divTargetPrice" runat="server" visible="false">
<input type="text" id="tbxPrice" runat="server" pattern="^\d+(?:\.\d\d?)?$" title="Valid amount in decimal number format (sample: 25.00)."
value='<%#DataBinder.Eval(Container.DataItem, "Price", "{0:f2}")%>' />
</div>
方法:
Protected Sub repeater_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repeater.ItemDataBound
dim strRptItem = e.Item.DataItem("RangePrice").ToString
Dim txtbox As New HtmlInputText
If String.IsNullOrEmpty(strRptItem) Then
e.Item.FindControl("divTargetPrice").Visible = True
Else
'e.Item.FindControl("divTargetPrice").Visible = False
txtbox = e.Item.FindControl("tbxPrice")
txtbox.Attributes.Add("display", "none")
End If
End If
End Sub
我尝试更改文本框的属性,但结果与隐藏的 div 相同。
【问题讨论】:
-
添加显示/隐藏文本框的代码。
-
如果它不是服务器端控件 (runat="server") 那么显示和隐藏将真的什么都不做。如果它对用户不可见,但仍在 HTML 代码中,则它存在,并且会被找到。
-
刚刚更新了我的帖子,在我需要简化代码时带了我。
标签: vb.net textbox repeater html-input