ASP.NET C# OnMouseOver 事件使列表框出现和消失
我有一个带有列表框和按钮的网络表单。 当 onmousover 事件触发时,列表框将出现; 但是在页面加载时,列表框不应该是可见的。 我有以下代码(请原谅背景颜色的变化):
Button2.Attributes.Add("onmouseout",
"this.style.backgroundColor='Blue',
ListBox3.style.display ='none'");
Button2.Attributes.Add("onmouseover",
"this.style.backgroundColor='Red',
ListBox3.style.display='block'");
当 listbox.visible 设置为 true 时,此代码有效。 不幸的是,当页面加载时,列表框始终可见,这是我想避免的。 当我将列表框设置为visible = false时,上面的代码不起作用。 我搞乱了回发并使用了 if 语句,例如 if (button = red), display=block; 然而,无济于事。 我被困在这一点上。 有谁知道我需要做哪些额外的事情才能使上述代码正常工作? 我是 ASP.NET 新手,所以我不知道是否还需要对 html 做一些事情。 另外,一个有趣的点是,代码的背景颜色部分工作完美。
我真的很感谢大家的帮助。
I have a single webform that has a listbox and a button. When the onmousover event fires the listbox will appear; however on page load, the listbox should not be visible. I have the following code (please excuse the background color changes):
Button2.Attributes.Add("onmouseout",
"this.style.backgroundColor='Blue',
ListBox3.style.display ='none'");
Button2.Attributes.Add("onmouseover",
"this.style.backgroundColor='Red',
ListBox3.style.display='block'");
This code works when the listbox.visible is set to true. Unfortunately, when the page loads, the listbox is always visible, which is what I want to avoid. When I set the listbox to visible = false, the above code doesn't work. I've messed around with postback and used if statements, such as if (button = red), display=block; however, to no avail. I am stuck at this point. Does anyone know what additional things I need to do to get the above code to work? I am new with ASP.NET, so I don't know if I also have to do something with the html. Also, one interesting point, the backgroundcolor portion of the code works flawlessly.
I really appreciate everyone's help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在服务器端代码中设置 Listboxvisible="false" 时,列表框的 HTML 不会呈现并发送到客户端。 因此,您需要确保列表框得到渲染,但设置为在页面加载时不可见,方法是使用
或者
另请参阅这篇显示与可见性文章
When you set Listbox visible="false" in the server-side code, the HTML for the listbox is not rendered and sent to the client. Therefore, you need to ensure that the listbox gets rendered, but is set to not be visible upon page load, either by
or
Also have a look at this Display vs. Visibility article
您可以使用 jQuery javascript 库轻松完成此操作。
您将拥有类似以下内容的代码,而不是您的代码:
悬停的第一个功能是鼠标悬停,第二个功能是鼠标悬停。 您还可以设置显示和隐藏的动画,使其看起来更光滑。
You could use the jQuery javascript library for doing this pretty easily.
Instead of your code you would have something like:
The first function of hover is a mouseover and second function is mouseout. You can also animate it showing and hiding which makes it look slicker.