How To Create a JavaScript Confirm inside a Condition in ASP.NET

  • Thread starter Joe Finsterwald
  • Start date
J

Joe Finsterwald

Recently I needed to add a confirm to a LinkButton that called a
JavaScript function on success, and did nothing on failure. I found
documentation on adding a confirm, but not on how to place a confirm
in a conditional. In any case, I've found the answer and I thought
I'd share.

In VB.NET (in the code behind in VS.NET)

Adding just a confirm...

Dim l As LinkButton
l.Attributes.Add("onclick", "return confirm('Delete this item?');")

Adding a confirm inside a conditional...

Dim m As LinkButton
m.Attributes.Add("onclick", "if (confirm('Approve this item?'))
yourFunction('yourArg') ; else return false ;")

In C# (in the code behind in VS.NET)

Adding just a confirm...

LinkButton l ;
l.Attributes.Add("onclick", "return confirm('Delete this item?');") ;

Adding a confirm inside a conditional...

LinkButton m ;
m.Attributes.Add("onclick", "if (confirm('Approve this item?'))
yourFunction('yourArg') ; else return false ;") ;

(the important thing is to return a boolean = false to prevent a page
reload.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top