使用 Chrome 并直接单击图像时,带有 Img 元素的自定义 ASP 按钮不会触发服务器端事件
我有一个自定义 ASP.NET 按钮,里面有一个图像元素(和一个 asp 文字)。我想要一个带有图像和文本的按钮,但我不想使用 CSS,因为使用 CSS 设置背景图像似乎会删除所有标准按钮格式(自动创建的渐变、边框等)。我将按钮连接到标准服务器端单击事件。我将按钮放置在 ASP:UpdatePanel 内。当我在 Chrome (v 12) 中打开网站并直接单击按钮上呈现的图像时,服务器端的单击事件不会触发。不过有一个回发。如果我使用 FireFox 或 IE,它工作正常(即,服务器端ConfirmButton_Click 触发)。如果我的按钮位于 UpdatePanel 之外,那么在 Chrome 中也可以。
代码(我省略了导入):
1.允许图像的自定义按钮:
namespace MyControlLibrary
{
/// <summary>
/// Render an HTML button instead of an input element.
/// </summary>
[ToolboxData("<{0}:HTMLButton runat=server></{0}:HTMLButton>")]
[ParseChildren(false)]
[PersistChildren(true)]
public class HTMLButton : Button
{
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Button;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
RenderChildren(output);
}
}
}
2.页面,代码隐藏:
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void ConfirmButton_Click(object sender, EventArgs e)
{
// would be nice to be able to do something here
}
}
}
3.ASP 标记:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<%@ Register Assembly="MyControlLibrary" Namespace="MyControlLibrary" TagPrefix="CUSTOM" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Always">
<ContentTemplate>
<CUSTOM:HTMLButton runat="server" ID="HTMLButton2" OnClick="ConfirmButton_Click"
Enabled="true">
<img id="Img1" runat="server" src="~/images/page_go.png" alt="some alt text" />
<asp:Literal ID="Literal1" runat="server" Text="Inside an ASP:UpdatePanel" />
</CUSTOM:HTMLButton>
</ContentTemplate>
</asp:UpdatePanel>
<CUSTOM:HTMLButton runat="server" ID="HTMLButton1" OnClick="ConfirmButton_Click"
Enabled="true">
<img id="Img2" runat="server" src="~/images/page_go.png" alt="some alt text" />
<asp:Literal ID="Literal2" runat="server" Text="Just on the page" />
</CUSTOM:HTMLButton>
</asp:Content>
更新:附加信息
上,我发现当我直接单击按钮上的图像时(从而导致问题),表单数据与单击非图像时略有不同按钮的区域。
ScriptManager1:UpdatePanel1|HTMLButton2
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE:/wEPDwUJNDk5NDQ3MTUyZGSFDQs1Xrosg9lxmcJ 3Q6Nz2 zbrq0CLJEKUy3yWQ0yw==
__EVENTVALIDATION:/wEWAwK8nZ3eAgKqrv38BgLZ8qO9CG7K1tlR9ucFA1AbifYgZtWmVKs/NshLxmxCD39QxIE9
__ASYNCPOST:true
HTMLButton2:
以上是我点击时看到的内容,不是图像上的内容。即,它会导致服务器端单击事件按照我的预期触发。当我直接单击图像时,ScriptManager1 和最后一行中缺少“HTMLButton2”。
I have a custom ASP.NET button with an image element inside it (and an asp literal). I want a button with an image an text, but I would prefer not to use CSS as setting the background image with CSS seems to remove all of the standard button formatting (gradients, borders, etc, that are automatically created). I have the button hooked up to a standard server-side click event. I have the button placed inside an ASP:UpdatePanel. When I open the site in Chrome (v 12) and click directly on the image rendered on the button, the click event on the server-side doesn't fire. There is a post-back though. If I use FireFox or IE, it works fine (ie, the server-side ConfirmButton_Click fires). And if I have the button outside of the UpdatePanel, it is also fine in Chrome.
Code (I left out the imports):
1.My custom button that allows an Image:
namespace MyControlLibrary
{
/// <summary>
/// Render an HTML button instead of an input element.
/// </summary>
[ToolboxData("<{0}:HTMLButton runat=server></{0}:HTMLButton>")]
[ParseChildren(false)]
[PersistChildren(true)]
public class HTMLButton : Button
{
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Button;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
RenderChildren(output);
}
}
}
2.The page, code-behind:
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void ConfirmButton_Click(object sender, EventArgs e)
{
// would be nice to be able to do something here
}
}
}
3.The ASP Markup:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<%@ Register Assembly="MyControlLibrary" Namespace="MyControlLibrary" TagPrefix="CUSTOM" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Always">
<ContentTemplate>
<CUSTOM:HTMLButton runat="server" ID="HTMLButton2" OnClick="ConfirmButton_Click"
Enabled="true">
<img id="Img1" runat="server" src="~/images/page_go.png" alt="some alt text" />
<asp:Literal ID="Literal1" runat="server" Text="Inside an ASP:UpdatePanel" />
</CUSTOM:HTMLButton>
</ContentTemplate>
</asp:UpdatePanel>
<CUSTOM:HTMLButton runat="server" ID="HTMLButton1" OnClick="ConfirmButton_Click"
Enabled="true">
<img id="Img2" runat="server" src="~/images/page_go.png" alt="some alt text" />
<asp:Literal ID="Literal2" runat="server" Text="Just on the page" />
</CUSTOM:HTMLButton>
</asp:Content>
Update: Additional Information
On the Network tab of the Chrome built-in debugger, I see that when I click directly on the image on the button (thus causing the problem), the Form Data is a little different than when I click on the non-image area of the button.
ScriptManager1:UpdatePanel1|HTMLButton2
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE:/wEPDwUJNDk5NDQ3MTUyZGSFDQs1Xrosg9lxmcJ 3Q6Nz2 zbrq0CLJEKUy3yWQ0yw==
__EVENTVALIDATION:/wEWAwK8nZ3eAgKqrv38BgLZ8qO9CG7K1tlR9ucFA1AbifYgZtWmVKs/NshLxmxCD39QxIE9
__ASYNCPOST:true
HTMLButton2:
Above is what I see when I click, not on the image. Ie, it causes the server-side click event to fire as I expect. When I click directly on the image, the "HTMLButton2" is missing from the ScriptManager1 and from the last line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到的解决方案是这样的:我添加了一个与按钮内 Img 的 onclick 绑定的客户端 JS/jQuery 函数。它取消当前事件,并单击父元素。这是代码:
事实上,我实际上注册了这个脚本,并从我的自定义 HTMLButton 控件中连接图像的 onclick,这样我就不必将脚本放在使用该按钮的每个页面中。不过,我认为没有必要发布这些详细信息。
Okay, the solution that I found was this: I added a client-side JS/jQuery function tied to onclick of the Img inside the button. It cancels the current event, and does a click on the parent element. Here is the code:
As a matter of fact, I actually register this script as well as hook up the onclick of the image from within my custom HTMLButton control so that I don't have to put the script in every page that uses the button. I didn't see the need to post those details though.