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