Client-Side Confirm

I'm stuck.

I have a asp:LinkButton which calls a code-behind sub-procedure when clicked. I need to add a client-side javascript-type confirm on the client-side to make sure this is what the user wants to do.

How can I pull this off?

If I create a java script function to place the confirm statement, how do I call the code-behind procedure from java-script?

Any help on this would be greatly appreciated...

8/24/2008 5:18:30 PM 91979 articles. 4 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 35
Get it on Google Play
Get it on Apple App Store


you can simply add

OnClientClick="if(!confirm('Are you sure?')){return false;}"

to the LinkButton markup. Don't confuse client-side with server-side. Your javascript does not need and literally saying, it cannot, call the server-side method. With the previous script, you're basically preventing a postback if confirm returns false. The check is there e.g no direct return confirm('...') because that would contradict with cross-page post scenarios (I've explained that in the past:


Teemu Keiski
Finland, EU
8/24/2008 6:16:59 PM

try this

<asp:HyperLink id="test" OnClientClick="return confirm('are you certain you want to do that?');" OnClick="ServerSideMethodToExecute" runat="server"></asp:HyperLink>

if the confirm returns false then a postback won't occur, if it returns true then a postback will occur and the server side onclick for the hyperlink control will be fired.

Community Coder
8/24/2008 6:23:21 PM

 Thanks very much, I will give your suggestions a try.

 Another problem I ran into is that I have a data grid with a button column with the ButtonType of LinkButton, CommandName of Delete, and Text of Delete.

When the user clicks this, I would also like to run the client-side javascript confirm before proceeding with the delete operation.

I tried to do this on the Page Load Code-Behind (dgCollateral is the ID of my DataGrid):

dgCollateral.Attributes.Add("DeleteCommand", _
"return confirm('Are you sure you wish to delete this collateral record?')")

While testing, when I click the Delete LinkButton, I don't receive the JavaScript confirm message box.

Any ideas?

8/24/2008 11:36:25 PM