javascript 使用 C# 的 ASP.net 中的图像按钮 OnClientClick() 事件问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14232731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image button OnClientClick() event issue in ASP.net using C#
提问by SuryaKavitha
I am having one Web page(First window). In that page I am having one image button, Which is having OnClientClick event. In that event I call a javascript method as OpenHelpWindow()
i.e.
我有一个网页(第一个窗口)。在那个页面中,我有一个图像按钮,它有 OnClientClick 事件。在那种情况下,我将 javascript 方法称为OpenHelpWindow()
ie
OnClientClick ="OpenHelpWindow()"
Now whenever I clicked that image button the event in the javascript method(OpenHelpWindow())
will get trigger and it will open the New help web window. Its working properly but the problem is, When I press Enter key then also the event will get fire and show me the help window.
Now I want to stop the second window (Help window) opening when the Enter key pressed.
现在,每当我单击该图像按钮时,javascript 方法中的事件(OpenHelpWindow())
都会被触发,并会打开“新建帮助 Web”窗口。它工作正常,但问题是,当我按下 Enter 键时,事件也会触发并显示帮助窗口。
现在我想在按下 Enter 键时停止打开第二个窗口(帮助窗口)。
How can I resolve this problem?
我该如何解决这个问题?
My Code for the first web window Image button is,
我的第一个 Web 窗口图像按钮的代码是,
.aspx code
.aspx 代码
.
.
<asp:ImageButton ImageUrl="~/Images/help.png" runat="server" ID="ibtnHelp"
AlternateText="Help" ToolTip="Help" OnClientClick="OpenHelpWindow()" />
.
.
Javascript code for OpenHelpWindow() is
OpenHelpWindow() 的 Javascript 代码是
function OpenHelpWindow()
{
var newWin = null;
try
{
newWin = window.open("../Common/OpenHelpUrl.aspx");
newWin.focus();
return false;
}
finally
{
newWin = null;
}
}
回答by Roy Dictus
Your ImageButton
is the form's default button, which means that when the user presses Enter, that button automagically gets "pressed". This is standard ASP.NET behavior.
您ImageButton
是表单的默认按钮,这意味着当用户按下 Enter 键时,该按钮会自动“按下”。这是标准的 ASP.NET 行为。
You can change this by adding the following attribute to your <ImageButton.../>
tag:
您可以通过向<ImageButton.../>
标签添加以下属性来更改此设置:
UseSubmitBehavior="False"
Good luck!
祝你好运!
EDIT
编辑
If this doesn't work (which it should!), then you can fool the browser by adding an invisible dummy button in your form:
如果这不起作用(应该这样做!),那么您可以通过在表单中添加一个不可见的虚拟按钮来欺骗浏览器:
<asp:Button ID="dummy" runat="server" Text="" OnClientClick="return false;" style="display:none;"/>
and then adding defaultButton="dummy"
to your <form />
.
然后添加defaultButton="dummy"
到您的<form />
.
回答by moh
Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
无效的回发或回调参数。在页面中使用配置或 <%@ Page EnableEventValidation="true" %> 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。