Question

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

<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.

Was it helpful?

Solution 2

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)?

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

OTHER TIPS

  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

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

and if you dnt want to call server method use this

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

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>

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()"

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:

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

There is no problem with your OnClientClick method 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.

You may need to enable java scripts for your browser

How to enable JavaScript in a web browser?

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

To inject a confirm script from a AJAX postback,

ScriptManager.RegisterOnSubmitStatement(btn, Page.GetType(), "confirm", "return confirm('Are you sure');");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top