LinkButton 的 OnClick 在 IE8 中不会触发
以下内容在 Chrome 中运行良好。
<asp:LinkButton runat="server" ID="lbEdit" OnClick="lbEdit_Click">
<button type="button" class="edit">
Edit
</button>
</asp:LinkButton>
这是按钮及其子类的 CSS。
button
{
padding: 3px;
}
button.edit
{
background:#3f6096;
border:none;
font:10px verdana;
color:#fff;
width:71px;
margin-top:3px;
}
正如你所看到的,没有什么特别的;只是颜色和美丽的东西。
我单击蓝色的“编辑”按钮,它会很好地触发 OnClick 回发......在 Chrome 中!
但如果我在 IE8 中做同样的事情,它什么也没做;甚至没有检测到点击。
为了帮助查明问题,我删除了标签并只保留了“编辑”一词,它在 IE8 中作为一个简单的带下划线的链接工作得很好;回发火灾。
那么,为什么 IE8 不能接受 LinkButton 中的任何内容呢?
The following works just fine in Chrome.
<asp:LinkButton runat="server" ID="lbEdit" OnClick="lbEdit_Click">
<button type="button" class="edit">
Edit
</button>
</asp:LinkButton>
And here is the CSS for button and its subclass.
button
{
padding: 3px;
}
button.edit
{
background:#3f6096;
border:none;
font:10px verdana;
color:#fff;
width:71px;
margin-top:3px;
}
As you can see, nothing special; just colors and beautiful things.
I click on the blue Edit button and it fires the OnClick postback just fine.... in Chrome!
But if I do the same in IE8, it just does nothing; doesn't even detect a click.
To help pinpoint the problem, I removed the tag and kept just the word "Edit", and it works just fine in IE8 as a simple underlined link; the postback fires.
So, Why can't IE8 accept anything within LinkButton?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要处理回发并且同时看起来像一个按钮,为什么要使用 LinkButton?为什么不把它变成一个按钮呢?在我看来,使用其中一种而不是另一种的唯一原因是外观。从功能上讲,它们都应该触发回发。
Why use a LinkButton if you need to handle a postback and look like a button at the same time? Why not make it just a Button? It seems to me that the only reason to use one over the other is appearance. Functionally, they're both supposed to trigger a postback.
您正在做的事情不是您应该做的事情,但我想说 IE 可能是这里表现最接近“正确”的一个。
您不应将按钮元素嵌套在提供链接的其他元素内。您只需从
LinkButton
内部删除button
,将 OnClick 附加到它上面,使样式正常工作,就可以了。我之所以说我认为 IE 的行为更接近“正确”,是因为它正在执行您在最内层标签上定义的操作;这没什么。 (实际上,您尚未将任何操作附加到
button
元素)。但实际上,无论如何,这都不应该在 HTML 中执行,因为我认为该行为一开始就没有定义。What you are doing isn't something you are supposed to do, but I would say that IE is probably the one behaving closest to 'correctly' here.
You should not nest button elements inside other elements that provide links. You can simply remove the
button
from inside theLinkButton
, attach the OnClick to it instead, make the styling work, and it'll be good.The reason I say I think IE is behaving closer to 'correctly' is because it's doing what you have defined on the innermost tag; which is nothing. (you have not attached any actions to the
button
element, in actuality). But really, it's not something you should do in HTML anyway, as I don't think the behavior is defined in the first place.你想在这里实现什么目标?
您有一个带有嵌套 HTML 按钮的服务器端链接按钮吗?
当您希望超链接能够执行回发时,LinkButton 非常有用。
你看过渲染的 HTML 吗?我不知道锚标记内有一个按钮是否是有效的 HTML。
如果您希望回发由特定的服务器端事件处理,只需使用 ASP:Button。
如果不这样做,只需使用常规 HTML 按钮即可。
What are you trying to achieve here?
You have a server-side link button, with a nested HTML button?
A LinkButton is useful when you want a hyperlink to be able to perform a postback.
Did you have a look at the rendered HTML? I don't know if having a button inside an anchor tag is even valid HTML.
If you want the postback to be handled by a particular server-side event, just use an ASP:Button.
If you dont, just use a regular HTML button.
您可以使用CssClass:
You can use CssClass: