使用 addAttributes 更改文本框的颜色

发布于 2024-12-09 00:54:08 字数 446 浏览 0 评论 0原文

我正在使用 addAttributes 更改表单上所有文本框的颜色,并且我正在编写:

If Not IsPostBack Then
   For Each CTL In Page.Controls
      if TypeOf CTL Is TextBox Then
         Dim Txt = New TextBox

         Txt.Attributes.Add("onMouseOver", "javascript:this.style.background='yellow';")
         Txt.Attributes.Add("onMouseOut", "javascript:this.style.background='white';")
      End If
   Next
End If

页面上没有错误,但它不起作用。有人可以帮忙吗?

I am using addAttributes to change the color of all the textboxes present on the form and I am writing:

If Not IsPostBack Then
   For Each CTL In Page.Controls
      if TypeOf CTL Is TextBox Then
         Dim Txt = New TextBox

         Txt.Attributes.Add("onMouseOver", "javascript:this.style.background='yellow';")
         Txt.Attributes.Add("onMouseOut", "javascript:this.style.background='white';")
      End If
   Next
End If

There is no error on the page, but it does not work. Is there anyone who can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-12-16 00:54:08

这里有两个问题。

首先,每次New TextBox 时都会创建一个新的TextBox。创建文本框并设置属性后,您似乎只是丢弃它而没有实际使用它。实际上,您应该直接使用 CTL,而不是尝试创建新实例 Txt

第二个问题是,您只是尝试设置页面的直接子控件的属性。假设您的页面有一些表格或面板,并且您的 TextBox 位于其中。您将无法访问它们。您将需要递归地遍历控制树并根据需要设置属性。

代码应该如下所示:

Protected Sub SetTextBoxColor(ByVal parentCtl As Control)
    For Each ctl In parentCtl.Controls
        If TypeOf ctl Is TextBox Then
            ctl.Attributes.Add("onMouseOver", "javascript:this.style.background='yellow';")
            ctl.Attributes.Add("onMouseOut", "javascript:this.style.background='white';")
        End If
        SetTextBoxColor(ctl)
    Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        SetTextBoxColor(Page)
    End If
End Sub

There's 2 problems here.

First, you're creating a new TextBox each time you New TextBox. After creating the TextBox and setting the attributes, you seem to just discard it without actually using it. You should actually be using CTL directly instead of attempting to create a new instance Txt.

The second problem is that, you are only attempting to set the attributes for the direct child controls of the page. Let's say if your page have some tables or panels, and you TextBox is in those. You wouldn't be able to access them. You will need to recursively go through the control tree and set the attributes as necessary.

The code should look something like this:

Protected Sub SetTextBoxColor(ByVal parentCtl As Control)
    For Each ctl In parentCtl.Controls
        If TypeOf ctl Is TextBox Then
            ctl.Attributes.Add("onMouseOver", "javascript:this.style.background='yellow';")
            ctl.Attributes.Add("onMouseOut", "javascript:this.style.background='white';")
        End If
        SetTextBoxColor(ctl)
    Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        SetTextBoxColor(Page)
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文