Very useful OnClientClick property for developers (in 2.0)

If we wanted to fire up a client-side code on 'onclick' of a Button control, currently in ASP.NET 1.0 and 1.1 by adding similar to the following code would helps!

protected override void OnPreRender(EventArgs e)
{

btnClick.Attributes.Add("onclick", "return confirm('Are you the people from Planet Earth?');");

}

Or we could also add the similar code in Page_Load!!

void Page_Load(object sender, EventArgs e)
{

btnClick.Attributes.Add("onclick", "return confirm('Are you the people from Planet Earth?');");

}


Just to find how Script Engine is generating (XX.Attributes.Add to the client side code) or Spitting the code to Client browser, I have added the code in OnPreRender and Page_Load, Guess what?? It's intelligent enfough and showed only once on client browser :)

OK.. Let's come to the main streem...to have onClick to the button, we have to write some code in 1.0 and 1.1 as shown above, there is not direct method for us to do it! But in 2.0, by using of OnClientClick attribute on Button control, we could generate onClick event on Client-Side!

The example would be as shown below:

<asp:button id="btnClick" runat="server" OnClientClick="return confirm('Are you the people from Planet Earth?');" text="Button"></asp:button>

Do you liked it!! Exited!! Wanted to use this!! Holde your breath and wait for 2.0 :)

By the way in future we might expect server side functions for messagebox/input on client side... For info see MessageBox and InputBox Support for ASP.NET from Code Behind @ labs

Published Sunday, October 24, 2004 9:24 PM by SreedharK

Comments

# re:Very useful OnClientClick property for developers (in 2.0)

Sunday, April 10, 2005 6:56 AM by TrackBack
^_^,Pretty Good!

# re: Very useful OnClientClick property for developers (in 2.0)

Thursday, August 16, 2007 4:30 PM by LK

This would be even better if you could show me how to control the server event too.

# re: Very useful OnClientClick property for developers (in 2.0)

Thursday, October 18, 2007 10:43 AM by YouHelped Me!

Hey thanks SreedharK,

this was really simple and it worked.

Any ideas as to how to make a loader image (like MS image visible once this happens too)

Many thanks

# re: Very useful OnClientClick property for developers (in 2.0)

Tuesday, November 20, 2007 3:30 AM by Sanjeev

It is gud and helpful. Thnx.

# re: Very useful OnClientClick property for developers (in 2.0)

Monday, January 28, 2008 1:21 AM by foddy

good

# re: Very useful OnClientClick property for developers (in 2.0)

Friday, February 15, 2008 3:43 AM by vijayanand

Really Nice.. Thanks a lot..

# re: Very useful OnClientClick property for developers (in 2.0)

Wednesday, December 17, 2008 5:42 AM by Chris

Thanks for this simple yet effective piece of code very useful for adding a level of user validation on button clicks especially if they are buried within datalists etc

# re: Very useful OnClientClick property for developers (in 2.0)

Thursday, January 22, 2009 11:20 AM by camila

really is very good

# re: Very useful OnClientClick property for developers (in 2.0)

Thursday, January 22, 2009 12:22 PM by dbelley_office

How can I use the message shown from a resource file ?

OnClientClick="return confirm('<%$ Resources:LacMessage, DeleteConfirmation %>');" />

That does not work ... the Resouces:..... appears as my question instead of showing the real resources file keyvalue.

# re: Very useful OnClientClick property for developers (in 2.0)

Sunday, March 22, 2009 4:40 AM by Anders

Dbelley office found a solution to the resources-problem:

www.vbforums.com/showthread.php

:)

# re: Very useful OnClientClick property for developers (in 2.0)

Monday, July 13, 2009 1:53 PM by NotSreedhar

Nice, thanks. I had found the example somewhere else, but they had left off the return portion. I'm using the 2.0 version,

<asp:button id="btnClick" runat="server" OnClientClick="return confirm('Are you the people from Planet Earth?');" text="Button"></asp:button>

If you leave the return portion out, the user still is presented with the message, and they can click ok or cancel, but the code still executes either way, so make sure you add the return!

# re: Very useful OnClientClick property for developers (in 2.0)

Wednesday, April 28, 2010 9:59 AM by pianocomposer

It works just fine in 3.5 as well. If you are upgrading a Classic ASP program, you can use this method along with a Server.Transfer() to do a form submission to another page that handles saving. The "are you sure" Javascript code is doing validation. It returns true if the code is okay. Otherwise it puts up the error message(s) and returns false.

# re: Very useful OnClientClick property for developers (in 2.0)

Thursday, September 16, 2010 3:01 AM by propertydealer

This is really interesting and helpful as I used it and I am benefited by this code. Moreover if you want to get more information or wants to contact real estate agents in order to buy affordable properties then can visit this site or can call on 0120-4338222 or 800-232-2343.

# re: Very useful OnClientClick property for developers (in 2.0)

Monday, December 20, 2010 11:28 AM by ipad app office

Nothing is impossible!

-----------------------------------

# re: Very useful OnClientClick property for developers (in 2.0)

Saturday, December 25, 2010 1:05 PM by zorka

I tried to use cinfirm message in GridView.

But the record has deleted any way.

I need to get the return value from the client to the GridView_RowDeleting event. Any suggestions?

# re: Very useful OnClientClick property for developers (in 2.0)

Friday, February 11, 2011 2:24 PM by anti virus software reviews

"Cool, there may be basically some excellent points on here a number of my readers will perhaps come across this appropriate, will deliver a hyperlink, several thanks."

# re: Very useful OnClientClick property for developers (in 2.0)

Thursday, February 24, 2011 10:34 PM by daniel

very useful, muy agradecido!!

# re: Very useful OnClientClick property for developers (in 2.0)

Tuesday, March 22, 2011 5:53 AM by Jer

the return value , can someone explain a bit, whether it is true or false from the confirm function

# re: Very useful OnClientClick property for developers (in 2.0)

Sunday, August 28, 2011 2:22 AM by tryecrot

Yes there should realize the opportunity to RSS commentary, quite simply, CMS is another on the blog.

# re: Very useful OnClientClick property for developers (in 2.0)

Tuesday, October 04, 2011 3:16 AM by Shridhar Donde

Hi This is good code for beginers to learn new things.

I want to create a site in share point on click of button.

Leave a Comment

(required) 
(required) 
(optional)
(required)