Avatar of uconnfb13
uconnfb13

asked on 

Call A Client Side Function Before a Server Side Function

Hi All,

Can anybody tell me if it is possible to run a client side function and then if a particular action occurs, a call to the server will be made and dealt with.

To put some context, I would like to have a "Delete" button on a screen that will eventually delete a record out of my database.  However, I would like to ask the user "Are you sure you would like to delete this record?" before doing so.  If they confirm, then I would like my C# server side functionality deleting the record to fire.  If they do not confirm, I would like nothing to happen.

Is this possible/does anybody know a link to a decent tutorial?

Thanks!
ASP.NETJavaScriptWeb Development

Avatar of undefined
Last Comment
uconnfb13
SOLUTION
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Rose Babu
Rose Babu
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
You could use OnClientClick method of any button or linkbutton.

<asp:Button
      ID="btnDelete"
      runat="server"
      Text="Delete"
      OnClientClick="MyClinetSideFunction()" OnClick="MyServerSideFunction()"
 />

The javascript code is:


function MyClinetSideFunction()
{
   val ret = confirm('Are you sure want to delete this record?');

    return ret;

}

From your client side function if it is returned true then MyServerSideFunction() will be triggered. Otherwise it won't get triggered.

I hope this helps.
Avatar of uconnfb13
uconnfb13

ASKER

Thanks for the reply guys!  In the previous methods, will the server side function still fire even if the return value is false?
Avatar of Rose Babu
Rose Babu
Flag of India image

if you use the below code and if you click no or function returned false then the server side function will not be fired.
<asp:Button 
      ID="btnDelete" 
      runat="server" 
      Text="Delete" 
      OnClientClick="return confirm('Are you sure want to delete this record?');" 
 />

Open in new window

ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of uconnfb13
uconnfb13

ASKER

Hi Guys,

Worked perfectly and a lot easier than expected!  Thanks for the help!
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo