4

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.

शेखर
  • 17,412
  • 13
  • 61
  • 117
Abbas
  • 4,948
  • 31
  • 95
  • 161
  • you havnt defined OnClick in both of those approaches, so he can't jump into OnClick – Postback Apr 30 '13 at 06:26
  • 1
    you are missing a `JavaScript` call you should try something like this `onclick="javascript:return confirm('Are you sure you want to delete this slide?')"` – MethodMan Apr 30 '13 at 06:26
  • 1
    I mean it just postbacks without firing OnClientClick Event – Abbas Apr 30 '13 at 06:27
  • Below? can you please let me know how? i have double quotes outside and single inside – Abbas Apr 30 '13 at 06:32
  • Content should be double quote and parameter should be single.Just posted my answer below.Try and let me know. – MahaSwetha Apr 30 '13 at 06:34
  • There is nothing essentially wrong with the declaration of the script - just tried it as is and it worked, so problem seems to be elsewhere. Are you sure you are not overriding this handler? Maybe in codebehind settings `Attributes["onclick"]`, or in javascript? – Andrei Apr 30 '13 at 06:35
  • No i haven't declared any attributes anywhere from the code behind – Abbas Apr 30 '13 at 06:39
  • @Abbas you not getting confirm window? – Damith Apr 30 '13 at 06:44
  • no, it seems like the issue is in IE only, in FF (2nd approach) is working fine. 1st still have issues in all browsers – Abbas Apr 30 '13 at 06:46
  • Can you post the HTML into that both link buttons are rendered? – Andrei Apr 30 '13 at 06:53
  • You guys can see the page in action: http://kb.dev.asentechdev1.com/slideshowadmin_v2.aspx, site requires credentials here are they: username - Developer (Only for IE its .\Developer) and password: plmnko-09, click on the Browse Thumbnail Button, it will just postbacks, its a linkbutton – Abbas Apr 30 '13 at 07:01

6 Answers6

3
  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;" 
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23
  • I tried this Still that too not working – Abbas Apr 30 '13 at 06:29
  • make function as above and try – Shafqat Masood Apr 30 '13 at 06:30
  • No its not working i made below function: function Navigate() { javascript: window.open("http://www.microsoft.com"); } and OnClientClick="javascript:Navigate()" but its not working – Abbas Apr 30 '13 at 06:31
  • OnClientClick="javascript:Navigate(); – Shafqat Masood Apr 30 '13 at 06:32
  • I also tried calling the OnClientClick from Page_Load: btn.OnClientClick = "javascript:return confirm('Are you sure?')" and btn.OnClientClick = "javascript:Navigate();" both does not works – Abbas Apr 30 '13 at 06:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29169/discussion-between-abbas-and-shafqat-masood) – Abbas Apr 30 '13 at 07:05
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.

Ronnie Togger
  • 136
  • 1
  • 3
  • Yes you were right, problem was elsewhere, i was having a function which was preventing anchor tag's onclick event. – Abbas May 03 '13 at 16:14
0

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>
MahaSwetha
  • 1,058
  • 1
  • 12
  • 21
0

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()"
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

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');");
Damith
  • 62,401
  • 13
  • 102
  • 153
0

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

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56