C# OnClientClick 不适用于 asp.net LinkBut​​ton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16293456/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:39:42  来源:igfitidea点击:

OnClientClick does not works on asp.net LinkButton

c#asp.netlinkbuttononclientclick

提问by Abbas

I have an asp.net linkbutton, which contains the OnClientClickproperty, however the function within the OnClientClicknever gets called, it directly jumps to OnClickfunction.
Below are the 2 ways I am using LinkButton as:

我有一个linkbutton包含OnClientClick属性的 asp.net ,但是OnClientClick从未调用过其中的函数,它直接跳转到OnClick函数。
以下是我使用 LinkBut​​ton 的两种方式:

<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server" 
     OnClientClick="return confirm('Are you sure you want to delete?');">
</asp:LinkButton>

and:

和:

<asp:LinkButton ID="lnkDelete" runat="server" 
     OnClientClick="return confirm('Are you sure you want to delete this slide?');" 
     CommandName="DeleteThumbnail" CommandArgument='<%# Container.DataItemIndex %>'>
   <asp:Image ImageUrl="~/images/delete.gif" ID="imgDelete" runat="server"></asp:Image>  
</asp:LinkButton>

Both of the approaches does not works.

这两种方法都行不通。

Can anyone please provide some solution for the same.

任何人都可以为此提供一些解决方案。

采纳答案by Ronnie Togger

There is most probably some other page element that is preventing this event from being fired.

很可能有一些其他页面元素阻止了这个事件被触发。

Do you have any other page elements that might interfere? Have you tried removing all other page elements but this one? Do you have some AJAX calls that might interfere? Have you tried this with a simple html element (not asp.net)?

您还有其他可能会干扰的页面元素吗?您是否尝试删除除此之外的所有其他页面元素?您是否有一些可能会干扰的 AJAX 调用?你有没有用一个简单的 html 元素(不是 asp.net)试过这个?

You are most probably doing everything fine in your link button but it seems like problem is elsewhere.

您很可能在链接按钮中一切正常,但似乎问题出在其他地方。

回答by Shafqat Masood

  OnClientClick="javascript:return confirmAction();"  

  function confirmAction() {  
      if(confirm('Are you sure you want to delete?')) {  
        // you clicked the OK button.  
        // you can allow the form to post the data.  
        return true;  
    }  
    else {  
        return false;  
        }  
    }  

implement the Onclick on the server side

在服务器端实现 Onclick

 protected void lnkdelete_Click(object sender, EventArgs e)  
 {  
 }  

and if you dnt want to call server method use this

如果您不想调用服务器方法,请使用它

   OnClientClick="javascript:confirmAction(); return false;" 

回答by MahaSwetha

Place it in single quotes like below,

将它放在单引号中,如下所示,

<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server" OnClientClick="return confirm('Are you sure you want to delete?');"></asp:LinkButton>

回答by ????

Use like this

像这样使用

 function Navigate() 
 { 
     javascript: window.open("microsoft.com"); 
     return false;
 }

and on clientclick as follows

并在客户端单击如下

OnClientClick="return javascript:Navigate()"

or even

甚至

 function Navigate() 
 { 
     window.open("microsoft.com"); 
     return false;
 }

 OnClientClick="return Navigate()"

回答by Damith

There is no problem with your OnClientClickmethod it should prompt confirm window. but you haven't specify the onclick event of the link button. So you will not able to get the event from code behind.

你的OnClientClick方法没有问题,它应该提示确认窗口。但是您还没有指定链接按钮的 onclick 事件。因此,您将无法从后面的代码中获取事件。

You may need to enable java scripts for your browser

您可能需要为浏览器启用 java 脚本

How to enable JavaScript in a web browser?

如何在 Web 浏览器中启用 JavaScript?

Are you using Ajax toolkit? Update panel? then you need to register script by using script manager

你在使用 Ajax 工具包吗?更新面板?然后您需要使用脚本管理器注册脚本

To inject a confirm script from a AJAX postback,

要从 AJAX 回发注入确认脚本,

ScriptManager.RegisterOnSubmitStatement(btn, Page.GetType(), "confirm", "return confirm('Are you sure');");

回答by Santosh Panda

Seems like you have Disabled the javascript in IE. Just enable it & you are good to go. You can follow this post to enable/disable the javascript in IE:

好像您在 IE 中禁用了 javascript。只需启用它,您就可以开始使用了。您可以按照这篇文章在 IE 中启用/禁用 javascript:

http://browsers.about.com/od/internetexplorertutorials/ss/disable-javascript-ie9.htm

http://browsers.about.com/od/internetexplorertutorials/ss/disable-javascript-ie9.htm