473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

adding javascript window.confirm to linkbutton

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

Aug 2 '07 #1
14 18204
"darrel" <no*****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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.
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

Aug 2 '07 #2
<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
Aug 2 '07 #3
"darrel" <no*****@nowhere.comwrote in message
news:u9**************@TK2MSFTNGP05.phx.gbl...
><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.
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

Aug 2 '07 #4
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
Aug 2 '07 #5
"darrel" <no*****@nowhere.comwrote in message
news:OC**************@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:LinkButtonmost certainly *does*
have an OnClientClick property:
http://msdn2.microsoft.com/en-us/lib...ck(vs.80).aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 2 '07 #6
"darrel" <no*****@nowhere.comwrote in message
news:Oe**************@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?');");
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 3 '07 #7
"darrel" <no*****@nowhere.comwrote in message
news:ux**************@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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 3 '07 #8
>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
Aug 3 '07 #9
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
Aug 3 '07 #10
"darrel" <no*****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>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.
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

Aug 3 '07 #11
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
Aug 3 '07 #12
"darrel" <no*****@nowhere.comwrote in message
news:eO**************@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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 3 '07 #13
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
Aug 3 '07 #14
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
Aug 3 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Phil Powell | last post by:
<script> <!-- function willProceedToPay() { var willPay = window.confirm('Vill du g' . String.fromCharCode(229) . ' till betaling?'); if (willPay) return true; return false; }
2
by: Mark Pappert | last post by:
Hi, I am trying to determine whether the status bar is visible or not .. is there a method to check the state? I did a google search and found info re: window.status.visible but it returns...
7
by: Tasha's Dad | last post by:
A description of the problem: 1) Go to a page with various settings and a timeout (forces re-login if over 10 minutes) 2) Before the timeout, make some changes to settings. 3) Press a "reset to...
2
by: Frank Oquendo | last post by:
I need to be call up a new page from a hyperlink in a DataGrid. Unfortunately, I'm not allowed to pass any querystring values so I decided to use a TemplateColumn containing a LinkButton instead of...
1
by: bbcrock | last post by:
Is it possible to send attributes into this function to change the print preferences from Portrait to Landscape with IE 6.0 set as the standard browser? Right now I'm using ...
1
by: Doo0592 | last post by:
Hi all, Judging by the topics in here this should be a snap for all of you! Can any one explain why when i use the window.confirm in an if else statement it behaves like this: if (var ==...
1
by: Jack | last post by:
Hi, I am new to .NET and need help with adding click event on LinkButton programatically. Please see the code below. I would like to add a click event on LinkButton returned from "Function...
4
by: Arnab das | last post by:
Below is the javascript code i am using function confirmDelete() { var returnValue = window.confirm("Deleting the current page. Continue?"); return returnValue; // var...
3
by: fishjelly | last post by:
How to open new window through linkButton without using javacript?...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.