Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

adding javascript window.confirm to linkbutton

7 views
Skip to first unread message

darrel

unread,
Aug 2, 2007, 5:37:56 PM8/2/07
to
Is it possible to pre-empt the javascript function used to do a postback
from a linkbutton?

I'd like to use linkbutton to delete a record and want to add a confirmation
box via javascript "are you sure you want to delete this record?" before it
executes the postback. The javascript is straightforward for this, but have
no idea how to have that intercept the post-back script.

-Darrel

Mark Rae [MVP]

unread,
Aug 2, 2007, 5:45:48 PM8/2/07
to
"darrel" <not...@nowhere.com> wrote in message
news:%23z18k1U...@TK2MSFTNGP02.phx.gbl...

Any JavaScript function which returns false will intercept a postback e.g.

<asp:Button ID="MyButton" runat="server" OnClick="MyButton_Click"
OnClientClick="return confirm('Are you sure you want to delete this
record?');" Text="Delete" />

In the above example, when a user clicks on the Delete button, they will be
prompted with a client-side Yes/No alert - if they click Yes, the postback
will happen and run the code in the server-side MyButton_Click method - if
they click No, the postback will be intercepted and nothing further will
happen...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

darrel

unread,
Aug 2, 2007, 6:04:58 PM8/2/07
to
> <asp:Button ID="MyButton" runat="server" OnClick="MyButton_Click"
> OnClientClick="return confirm('Are you sure you want to delete this
> record?');" Text="Delete" />

Slick, but can this be done with a linkButton? I don't see an OnClientClick
property for that.

-Darrel


Mark Rae [MVP]

unread,
Aug 2, 2007, 6:11:58 PM8/2/07
to
"darrel" <not...@nowhere.com> wrote in message
news:u98mrEV1...@TK2MSFTNGP05.phx.gbl...

Yep - in the Page_Load, add the following code:

MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
want to delete this record?');");

darrel

unread,
Aug 2, 2007, 6:23:06 PM8/2/07
to
> MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
> want to delete this record?');");

Hmm...I've done that, but the event handler still executes for the
linkButton regardless of whether I click OK or CANCEL in the confirmation
window.

-Darrel


Mark Rae [MVP]

unread,
Aug 2, 2007, 6:34:44 PM8/2/07
to
"darrel" <not...@nowhere.com> wrote in message
news:OCA$zOV1HH...@TK2MSFTNGP03.phx.gbl...

Actually, having just tried this, <asp:LinkButton> most certainly *does*
have an OnClientClick property:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick(vs.80).aspx

Collin Chung

unread,
Aug 2, 2007, 9:38:31 PM8/2/07
to
On Aug 3, 6:34 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "darrel" <notr...@nowhere.com> wrote in message

>
> news:OCA$zOV1HH...@TK2MSFTNGP03.phx.gbl...
>
> >> MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
> >> want to delete this record?');");
>
> > Hmm...I've done that, but the event handler still executes for the
> > linkButton regardless of whether I click OK or CANCEL in the confirmation
> > window.
>
> Actually, having just tried this, <asp:LinkButton> most certainly *does*
> have an OnClientClick property:http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.li...
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net


Hi guys, maybe Darrel is not using asp.net version 2.0, the msdn link
above says the onclientclick property is new for .Net framework
version 2.0 upwards. Anyway, to achieve the same effect using
javascript:

<script>
var mybutton = document.getElementById('linkButton1');
var oldonclickfunction = mybutton.onclick; //old postback method
mybutton.onclick=function() {
if(confirm('Are you sure you want to delete this record?'))
{oldonclickfunction();}
else{ return false;}
}
</script>

just need to replace linkButton1 with the button's id (extra caution
to make sure it's the control's unique hierarchically qualified id,
should be fine if the button is just on the form and not encapsulated
in a panel, table, grid or user control).

--
Collin

darrel

unread,
Aug 3, 2007, 10:44:53 AM8/3/07
to
> Hi guys, maybe Darrel is not using asp.net version 2.0

Yes, apologies for not clarifying that.

This is still a 1.1 app we're maintaining.

2.0 is moving along with MOSS, but for now, we're still going to be doing a
chunk of 1.1 work.

> Anyway, to achieve the same effect using
> javascript:
>
> <script>
> var mybutton = document.getElementById('linkButton1');
> var oldonclickfunction = mybutton.onclick; //old postback method
> mybutton.onclick=function() {
> if(confirm('Are you sure you want to delete this record?'))
> {oldonclickfunction();}
> else{ return false;}
> }
> </script>
>
> just need to replace linkButton1 with the button's id (extra caution
> to make sure it's the control's unique hierarchically qualified id,
> should be fine if the button is just on the form and not encapsulated
> in a panel, table, grid or user control).

Collin:

Don't I still need to apply some sort of onClick event to the linkButton,
though? I'm not sure how the above script would know to intercept the
onClick event if there's nothing to trigger it.

-Darrel


Mark Rae [MVP]

unread,
Aug 3, 2007, 10:49:05 AM8/3/07
to
"darrel" <not...@nowhere.com> wrote in message
news:Oe8lbzd1...@TK2MSFTNGP05.phx.gbl...

> Don't I still need to apply some sort of onClick event to the linkButton,
> though? I'm not sure how the above script would know to intercept the
> onClick event if there's nothing to trigger it.

MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
want to delete this record?');");

darrel

unread,
Aug 3, 2007, 10:55:50 AM8/3/07
to

> MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
> want to delete this record?');");

Well, this is where my lack of Javascript skills begins to show. ;0)

Here's the function I'm using curtesy of Collin:

var mybutton = document.getElementById('lnkBtn_delete');


var oldonclickfunction = mybutton.onclick; //old postback method
mybutton.onclick=function() {
if(confirm('Are you sure you want to delete this record?'))
{oldonclickfunction();}
else{ return false;}
}

Right now, that's giving me a 'mybutton has no properties' javascript error.

Mark, your onclick event makes sense, but that same confirm is also
executing in the script of collins, so it appears that I'm going to cause
double-alerts in this case (provided I can get the 'mybutton has no
properties' error fixed...)

-Darrel


Mark Rae [MVP]

unread,
Aug 3, 2007, 11:10:52 AM8/3/07
to
"darrel" <not...@nowhere.com> wrote in message
news:uxhAj5d1...@TK2MSFTNGP05.phx.gbl...

> Well, this is where my lack of Javascript skills begins to show. ;0)
>
> Here's the function I'm using curtesy of Collin:

> Mark, your onclick event makes sense,

You can't have both...

Andrew Morton

unread,
Aug 3, 2007, 11:29:27 AM8/3/07
to
darrel wrote:
>> MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure
>> you want to delete this record?');");
>
> Well, this is where my lack of Javascript skills begins to show. ;0)
>
> Here's the function I'm using curtesy of Collin:
>
> var mybutton = document.getElementById('lnkBtn_delete');
> var oldonclickfunction = mybutton.onclick; //old postback method

Isn't JavaScript case-sensitive, so onclick should be onClick?

Andrew


darrel

unread,
Aug 3, 2007, 12:04:42 PM8/3/07
to
>> Here's the function I'm using curtesy of Collin:
>> Mark, your onclick event makes sense,
>
> You can't have both...

Right. But that's kind of going back to my original problem...adding the
confirmation on the OnClick event of the linkbutton does NOT intercept the
postback. Regardless of whether or not I click OK or CANCEL, the postback is
still executed.

-Darrel


darrel

unread,
Aug 3, 2007, 12:11:06 PM8/3/07
to
> Isn't JavaScript case-sensitive, so onclick should be onClick?

'doh! yea, good catch!

That said, I still get the 'mybutton has no properties' error.

-Darrel


Mark Rae [MVP]

unread,
Aug 3, 2007, 12:59:37 PM8/3/07
to
"darrel" <not...@nowhere.com> wrote in message
news:%23mxBCge...@TK2MSFTNGP06.phx.gbl...

It's working perfectly for me, although I am of course using ASP.NET v2. I
don't recall there being any problems in v1.x, though... Here's the code I'm
using:

<asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click"
OnClientClick="return confirm('Are you sure you want to delete this
record?');" Text="Delete" />

protected void lnkButton_Click(object sender, EventArgs e)
{
string strTest = String.Empty;

darrel

unread,
Aug 3, 2007, 1:04:47 PM8/3/07
to
> It's working perfectly for me, although I am of course using ASP.NET v2. I
> don't recall there being any problems in v1.x, though... Here's the code
> I'm using:
>
> <asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click"
> OnClientClick="return confirm('Are you sure you want to delete this
> record?');" Text="Delete" />

Seems like 2.0 has a more intuitive 'OnClientClick' event.

If I apply the confirmation to the OnClick event in 1.1, the alert box
works...it just doesn't stop the postback from executing.

-Darrel


Mark Rae [MVP]

unread,
Aug 3, 2007, 2:30:20 PM8/3/07
to
"darrel" <not...@nowhere.com> wrote in message
news:eO4$mBf1HH...@TK2MSFTNGP03.phx.gbl...

> Seems like 2.0 has a more intuitive 'OnClientClick' event.
>
> If I apply the confirmation to the OnClick event in 1.1, the alert box
> works...it just doesn't stop the postback from executing.

Fair enough - unfortunately, I can't confirm this as I retired my one
remaining v1.1 installation quite a few months ago...

darrel

unread,
Aug 3, 2007, 3:05:52 PM8/3/07
to

> Fair enough - unfortunately, I can't confirm this as I retired my one
> remaining v1.1 installation quite a few months ago...

I don't blame you. ;o)

-Darrel


darrel

unread,
Aug 3, 2007, 4:51:18 PM8/3/07
to
> If I apply the confirmation to the OnClick event in 1.1, the alert box
> works...it just doesn't stop the postback from executing.

...CUASE I'M AN IDIOT!

doh'...finally realized it wasn't working because I forgot to put the RETURN
in there before the confirm command.

Sorry...USER ERROR!

Thanks for all the help Mark and Collin!

-Darrel


Vijay Kumar

unread,
Oct 22, 2010, 10:15:03 AM10/22/10
to
Can you send the Full Code ( Client Side and Server Side Code )

> On Thursday, August 02, 2007 5:37 PM darrel wrote:

> Is it possible to pre-empt the javascript function used to do a postback
> from a linkbutton?
>
> I'd like to use linkbutton to delete a record and want to add a confirmation
> box via javascript "are you sure you want to delete this record?" before it
> executes the postback. The javascript is straightforward for this, but have
> no idea how to have that intercept the post-back script.
>

> -Darrel


>> On Thursday, August 02, 2007 5:45 PM Mark Rae [MVP] wrote:

>> "darrel" <not...@nowhere.com> wrote in message

>> news:%23z18k1U...@TK2MSFTNGP02.phx.gbl...


>>
>>
>> Any JavaScript function which returns false will intercept a postback e.g.
>>

>> <asp:Button ID="MyButton" runat="server" OnClick="MyButton_Click"

>> OnClientClick="return confirm('Are you sure you want to delete this
>> record?');" Text="Delete" />
>>

>> In the above example, when a user clicks on the Delete button, they will be
>> prompted with a client-side Yes/No alert - if they click Yes, the postback
>> will happen and run the code in the server-side MyButton_Click method - if
>> they click No, the postback will be intercepted and nothing further will
>> happen...
>>
>>

>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net


>>> On Thursday, August 02, 2007 6:04 PM darrel wrote:

>>> Slick, but can this be done with a linkButton? I do not see an OnClientClick
>>> property for that.
>>>
>>> -Darrel


>>>> On Thursday, August 02, 2007 6:11 PM Mark Rae [MVP] wrote:

>>>> "darrel" <not...@nowhere.com> wrote in message

>>>> news:u98mrEV1...@TK2MSFTNGP05.phx.gbl...


>>>>
>>>>
>>>> Yep - in the Page_Load, add the following code:
>>>>

>>>> MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
>>>> want to delete this record?');");


>>>>
>>>>
>>>> --
>>>> Mark Rae
>>>> ASP.NET MVP
>>>> http://www.markrae.net


>>>>> On Thursday, August 02, 2007 6:23 PM darrel wrote:

>>>>> Hmm...I have done that, but the event handler still executes for the


>>>>> linkButton regardless of whether I click OK or CANCEL in the confirmation
>>>>> window.
>>>>>

>>>>> -Darrel


>>>>>> On Thursday, August 02, 2007 6:34 PM Mark Rae [MVP] wrote:

>>>>>> "darrel" <not...@nowhere.com> wrote in message

>>>>>> news:OCA$zOV1HH...@TK2MSFTNGP03.phx.gbl...


>>>>>>
>>>>>>
>>>>>> Actually, having just tried this, <asp:LinkButton> most certainly *does*
>>>>>> have an OnClientClick property:

>>>>>> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick(vs.80).aspx


>>>>>>
>>>>>>
>>>>>> --
>>>>>> Mark Rae
>>>>>> ASP.NET MVP
>>>>>> http://www.markrae.net


>>>>>>> On Thursday, August 02, 2007 9:38 PM Collin Chung wrote:

>>>>>>> On Aug 3, 6:34 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
>>>>>>>
>>>>>>>
>>>>>>> Hi guys, maybe Darrel is not using asp.net version 2.0, the msdn link
>>>>>>> above says the onclientclick property is new for .Net framework

>>>>>>> version 2.0 upwards. Anyway, to achieve the same effect using
>>>>>>> javascript:
>>>>>>>
>>>>>>> <script>
>>>>>>> var mybutton = document.getElementById('linkButton1');


>>>>>>> var oldonclickfunction = mybutton.onclick; //old postback method

>>>>>>> mybutton.onclick=function() {
>>>>>>> if(confirm('Are you sure you want to delete this record?'))
>>>>>>> {oldonclickfunction();}
>>>>>>> else{ return false;}
>>>>>>> }

>>>>>>> </script>
>>>>>>>
>>>>>>> just need to replace linkButton1 with the button's id (extra caution
>>>>>>> to make sure it's the control's unique hierarchically qualified id,
>>>>>>> should be fine if the button is just on the form and not encapsulated
>>>>>>> in a panel, table, grid or user control).
>>>>>>>

>>>>>>> --
>>>>>>> Collin


>>>>>>>> On Friday, August 03, 2007 10:44 AM darrel wrote:

>>>>>>>> Yes, apologies for not clarifying that.
>>>>>>>>
>>>>>>>> This is still a 1.1 app we're maintaining.
>>>>>>>>
>>>>>>>> 2.0 is moving along with MOSS, but for now, we're still going to be doing a
>>>>>>>> chunk of 1.1 work.
>>>>>>>>
>>>>>>>>

>>>>>>>> Collin:


>>>>>>>>
>>>>>>>> Don't I still need to apply some sort of onClick event to the linkButton,
>>>>>>>> though? I'm not sure how the above script would know to intercept the
>>>>>>>> onClick event if there's nothing to trigger it.
>>>>>>>>

>>>>>>>> -Darrel


>>>>>>>>> On Friday, August 03, 2007 10:49 AM Mark Rae [MVP] wrote:

>>>>>>>>> MyLinkButton.Attributes.Add("onclick", "return confirm('Are you sure you
>>>>>>>>> want to delete this record?');");


>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Mark Rae
>>>>>>>>> ASP.NET MVP
>>>>>>>>> http://www.markrae.net


>>>>>>>>>> On Friday, August 03, 2007 10:55 AM darrel wrote:

>>>>>>>>>> Well, this is where my lack of Javascript skills begins to show. ;0)
>>>>>>>>>>

>>>>>>>>>> Here's the function I'm using curtesy of Collin:
>>>>>>>>>>

>>>>>>>>>> var mybutton = document.getElementById('lnkBtn_delete');
>>>>>>>>>> var oldonclickfunction = mybutton.onclick; //old postback method

>>>>>>>>>> mybutton.onclick=function() {
>>>>>>>>>> if(confirm('Are you sure you want to delete this record?'))
>>>>>>>>>> {oldonclickfunction();}
>>>>>>>>>> else{ return false;}
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> Right now, that's giving me a 'mybutton has no properties' javascript error.
>>>>>>>>>>
>>>>>>>>>> Mark, your onclick event makes sense, but that same confirm is also
>>>>>>>>>> executing in the script of collins, so it appears that I'm going to cause

>>>>>>>>>> double-alerts in this case (provided I can get the 'mybutton has no
>>>>>>>>>> properties' error fixed...)
>>>>>>>>>>
>>>>>>>>>> -Darrel


>>>>>>>>>>> On Friday, August 03, 2007 11:10 AM Mark Rae [MVP] wrote:

>>>>>>>>>>> You cannot have both...


>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> Mark Rae
>>>>>>>>>>> ASP.NET MVP
>>>>>>>>>>> http://www.markrae.net


>>>>>>>>>>>> On Friday, August 03, 2007 11:29 AM Andrew Morton wrote:

>>>>>>>>>>>> darrel wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> Isn't JavaScript case-sensitive, so onclick should be onClick?
>>>>>>>>>>>>

>>>>>>>>>>>> Andrew


>>>>>>>>>>>>> On Friday, August 03, 2007 12:04 PM darrel wrote:

>>>>>>>>>>>>> Right. But that's kind of going back to my original problem...adding the
>>>>>>>>>>>>> confirmation on the OnClick event of the linkbutton does NOT intercept the
>>>>>>>>>>>>> postback. Regardless of whether or not I click OK or CANCEL, the postback is
>>>>>>>>>>>>> still executed.
>>>>>>>>>>>>>

>>>>>>>>>>>>> -Darrel


>>>>>>>>>>>>>> On Friday, August 03, 2007 12:11 PM darrel wrote:

>>>>>>>>>>>>>> 'doh! yea, good catch!
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> That said, I still get the 'mybutton has no properties' error.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> -Darrel


>>>>>>>>>>>>>>> On Friday, August 03, 2007 12:59 PM Mark Rae [MVP] wrote:

>>>>>>>>>>>>>>> "darrel" <not...@nowhere.com> wrote in message

>>>>>>>>>>>>>>> news:%23mxBCge...@TK2MSFTNGP06.phx.gbl...


>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> It's working perfectly for me, although I am of course using ASP.NET v2. I
>>>>>>>>>>>>>>> don't recall there being any problems in v1.x, though... Here's the code I'm
>>>>>>>>>>>>>>> using:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> <asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click"
>>>>>>>>>>>>>>> OnClientClick="return confirm('Are you sure you want to delete this
>>>>>>>>>>>>>>> record?');" Text="Delete" />
>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>> protected void lnkButton_Click(object sender, EventArgs e)
>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>> string strTest = String.Empty;
>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>> --
>>>>>>>>>>>>>>> Mark Rae
>>>>>>>>>>>>>>> ASP.NET MVP
>>>>>>>>>>>>>>> http://www.markrae.net


>>>>>>>>>>>>>>>> On Friday, August 03, 2007 1:04 PM darrel wrote:

>>>>>>>>>>>>>>>> Seems like 2.0 has a more intuitive 'OnClientClick' event.
>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>> If I apply the confirmation to the OnClick event in 1.1, the alert box

>>>>>>>>>>>>>>>> works...it just does not stop the postback from executing.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> -Darrel


>>>>>>>>>>>>>>>>> On Friday, August 03, 2007 2:30 PM Mark Rae [MVP] wrote:

>>>>>>>>>>>>>>>>> Fair enough - unfortunately, I cannot confirm this as I retired my one


>>>>>>>>>>>>>>>>> remaining v1.1 installation quite a few months ago...
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>> --
>>>>>>>>>>>>>>>>> Mark Rae
>>>>>>>>>>>>>>>>> ASP.NET MVP
>>>>>>>>>>>>>>>>> http://www.markrae.net


>>>>>>>>>>>>>>>>>> On Friday, August 03, 2007 3:05 PM darrel wrote:

>>>>>>>>>>>>>>>>>> I do not blame you. ;o)
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> -Darrel


>>>>>>>>>>>>>>>>>>> On Friday, August 03, 2007 4:51 PM darrel wrote:

>>>>>>>>>>>>>>>>>>> ...CUASE I'M AN IDIOT!
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> doh'...finally realized it wasn't working because I forgot to put the RETURN
>>>>>>>>>>>>>>>>>>> in there before the confirm command.
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Sorry...USER ERROR!
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Thanks for all the help Mark and Collin!
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> -Darrel


>>>>>>>>>>>>>>>>>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>>>>>>>>>>>>>>>>>> Silverlight 4 Martin Fractals with WriteableBitmapEx
>>>>>>>>>>>>>>>>>>> http://www.eggheadcafe.com/tutorials/aspnet/d312fb6f-9610-449f-a191-26fcede58730/silverlight-4-martin-fractals-with-writeablebitmapex.aspx

0 new messages