【问题标题】:Textbox inside a repeater even though it's hidden it is still being found, why?转发器内的文本框即使隐藏了它仍然可以找到,为什么?
【发布时间】: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


【解决方案1】:

在您的 For-Each 外观中,与其查找 tbx is not nothing,不如查找具有属性 display=none 的。

【讨论】:

    猜你喜欢
    • 2015-05-28
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多