This is a migrated thread and some comments may be shown as answers.

OnClientClick confirm delete

13 Answers 3468 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 11 Mar 2009, 04:12 PM
All,

I have a delete button inside a RadAjaxPanel:

<asp:Button id="btnDelete" Text="Delete" OnClientClick="return confirm('Are you sure you want delete this?');" runat="server"/>

The js popup is working, but it is not running the server-side click event attached to button.  Take away the OnClientClick, the server-side event runs fine.

What am I doing wrong?

Cheers,
Dave

13 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 13 Mar 2009, 11:53 AM
Hello David,

Please try modifying your code as below:

<asp:Button id="btnDelete" Text="Delete" runat="server"/  
   OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;"/> 

Find more about client confirmation and ajax here.

Kind regards,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Andreas Botsikas
Top achievements
Rank 2
answered on 29 Apr 2009, 10:19 AM
Hi,

I would like to translate this message: 'Are you sure you want to delete this?' in the languages my site uses. So could you tell me how can I modify the following statement using Resources but only for the question and not for the whole string.

OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;"

 
 Thanksss,
Andreas.




0
Iana Tsolova
Telerik team
answered on 29 Apr 2009, 11:11 AM
Hi Andreas,

In this case you could assign the button OnClientClick property server side, for instance as below:

protected viod Page_Load(object, sender, EventArgs e)     
{     
    string deleteMessage = "";     
    //retrieve the delere message from the Resource file     
    btnDelete.OnClientClick = "if(!confirm('" + delteMessage + "')) return false;";     
}   

I hope this helps.

All the best,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Tonino
Top achievements
Rank 1
answered on 09 Jun 2009, 01:20 PM
Hello

I'm using this approach in the aspx file:
 OnClientClick=<%# String.Format("if(!confirm('{0}'))return false;", Resources.GlobalRes.btnDeleteSelected_ConfirmText)%>

It works when you do a DataBind() in the code behind eg. in page_load.

Tonino.



0
Eliza Sahoo
Top achievements
Rank 1
answered on 17 Feb 2010, 11:02 AM
Hi all,
    1. Here we have a function The 'ConfirmDelete()' which is executed each time a click event occurs on the page.
2. Then we use a logic that to find which element was clicked on i.s we append a word 'DELETE' to the ID of the control.
3.And finally check if the clicked element having ID is 'DELETED' , then the javascript function will be execute to ask the user about his/her confirmation.
 
JavaScript code:
 
 function ConfirmDelete(e) 
{    
var targ;

if (!e)
{   
var e = window.event;
}
targ = (e.target) ? e.target : e.srcElement;

if (targ.nodeType == 3) 
{
targ = targ.parentNode;
}

if (targ.id.toLowerCase().indexOf("delete") >= 0) 
{        
return confirm("Do you want to delete this item?");
}
routeEvent(e);
}  
document.onclick = ConfirmDelete;
 
aspx  code:

<asp:GridView RunAt="server" ID="gvTest" DataSourceID="SqlDataSource1">
<Columns>       
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" 
ID="DeleteMe" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 Hope this was helpful.

0
Bala
Top achievements
Rank 1
answered on 21 Jun 2012, 07:40 AM
<asp:Button id="btnDelete" Text="Delete" runat="server"/  
   OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;"/> 

this worked for me, my page is surrounded by radajaxpanel with radloading panel & also i have server side onclick event for a button.

The above one worked for me rather than using btn.Attributes.Add("OnClick","return confirm();"); It invokes the condifrm prompt and after receiving the request it triggers the server side code for that button.
0
Elias
Top achievements
Rank 2
answered on 03 May 2013, 06:29 PM
Hello, I try this scenario but a little bit different, I want to the confirm window looks as telerik radconfirm. I change the code loke this:
<asp:Button ID="btnDelete" runat="server"="if(!radconfirm('Are you sure you want delete this?')) return false;" Text="DELETE"    />

The radconfirm shows but disappear matters of a blink of the eye.  I don't know why. Any thoughts? my version is 2010.1.415.35
0
Shinu
Top achievements
Rank 2
answered on 06 May 2013, 06:38 AM
Hi,

Try modifying the code as shown below.
aspx:
<asp:Button ID="btnDelete" Text="Delete" runat="server" OnClientClick="ShowDialog(this); return false;"/>
JS:
function ShowDialog(btn) {
       radconfirm("are you sure you want to delete this?");
   }

Thanks,
Shinu
0
Elias
Top achievements
Rank 2
answered on 17 May 2013, 03:34 PM
Thanks for the reply. The dialog works but when I press the Ok button not fire the btnDelete_Click
event

aspx
<asp:Button ID="btnDelete" Text="Delete" runat="server" OnClientClick="ShowDialog(this); return false;" OnClick="btnDelete_Click"/>

jscript
function ShowDialog(btn) {
    radconfirm("are you sure you want to delete this?");

c#
protected void btnDelete_Click(object sender, EventArgs e)
       {
          DeleteRecord()
       }

any thoughts?
0
Edward
Top achievements
Rank 1
answered on 19 May 2013, 02:27 AM
function OnClientClicked(button, args)
{
    if (window.confirm("Do you want to do this postback?"))
    {
        button.set_autoPostBack(true);
    }
    else
    {
        button.set_autoPostBack(false);
    }
}

You can use the function above, and 
OnClientClick="ShowDialog(this); return false;" 
should be
OnClientClicked="OnClientClicked"

-Ed
0
Elias
Top achievements
Rank 2
answered on 20 May 2013, 01:33 PM
Thanks for the reply, do all respect, this workaround does not work. onClientClicked is not an event for asp button or maybe I dont understand what you can achieve with this solution.
0
Angel Petrov
Telerik team
answered on 22 May 2013, 02:07 PM
Hi Elias,

Actually Ed was probably referring to the OnClientClicked event of the RadButton. In attachments you can find a runnable sample which demonstrates a possible implementation of the described scenario.

Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Elias
Top achievements
Rank 2
answered on 22 May 2013, 03:30 PM
Oh, ok. Thanks for the reply. I use a quite old dll (2010) for that reason I use the asp button, (I dont have the radbutton in that dll.) but thanks anyways
Tags
Ajax
Asked by
David
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Andreas Botsikas
Top achievements
Rank 2
Tonino
Top achievements
Rank 1
Eliza Sahoo
Top achievements
Rank 1
Bala
Top achievements
Rank 1
Elias
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Edward
Top achievements
Rank 1
Angel Petrov
Telerik team
Share this question
or