使用 addAttributes 更改文本框的颜色
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个问题。
首先,每次
New TextBox
时都会创建一个新的TextBox。创建文本框并设置属性后,您似乎只是丢弃它而没有实际使用它。实际上,您应该直接使用CTL
,而不是尝试创建新实例Txt
。第二个问题是,您只是尝试设置页面的直接子控件的属性。假设您的页面有一些表格或面板,并且您的 TextBox 位于其中。您将无法访问它们。您将需要递归地遍历控制树并根据需要设置属性。
代码应该如下所示:
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 usingCTL
directly instead of attempting to create a new instanceTxt
.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: