Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 4th, 2004, 05:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting e.g. 'onclick' on button

I am trying to do a setup where I would like to show only some elements on the page at startup and if some button is clicked show some other stuff. All this without reloading the page; that is, I would like to use java script for toggleing the visibility (CSS) of the elements, but construct the user interface from code behind.

Is it possible in some way to define that a HtmlInputButton should call some java script function (client side)!? As far as I have understood, when I make a
Code:
HtmlInputButton btn = new HtmlInputButton();
then it is equivalent to doing something like...
Code:
<INPUT type='button' runat='server'>
right?! Then how do I set the 'onclick' equivalent? Or is this impossible?

Thanks

Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old February 4th, 2004, 06:19 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hmmm, what's the point of a *server* side button that does nothing but execute code at the *client*? In that case, why not just an plain ol' HTML button? (input type="button")

An HtmlInputButton will end up as a "input type="submit" button in the browser, so the form gets submitted when you click it. Just as with plain HTML buttons, you can add some code that fires when you click the button. You can use the Attributes collection of the button to add some code to the click handler:
Code:
myDeleteButton.Attributes.Add("onclick", "return(confirm('Are you sure you want to delete this item?'));");
This code will stop the form from being submitted when the user presses the cancel button.
You can use the same technique to call JavaScript functions embedded in the page or in a separate .js file:
Code:
myOtherButton.Attributes.Add("onclick", "return MyJSFunction();");
If you make sure that MyJSFunction returns false, the page will never be submitted to the server, and the button is pure client side.

However, I think this stuff is more confusing than useful. If you do only need a client side button, use a simple HTML button.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 4th, 2004, 07:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar... once again a thorough answer!

The thing is that I am generating a questionnaire dynamically from data entered in an XML file, and I want to flip pages using java script.

I believe that when you use HtmlInputButton like this
Code:
new HtmlInputButton("button")
you will actually get a button that does not submit the form.

So I think I can do it this way and use the Attributes.Add method. I will try and then let the forum know.

Thanks again

Jacob.
 
Old February 4th, 2004, 07:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works! Thanks a lot again.
Code:
HtmlInputButton btn = new HtmlInputButton("button");
btn.Attributes.Add("onclick", "test(); return false;");
Jacob.
 
Old February 4th, 2004, 07:49 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, nice solution. I thought that a new HtmlInputButton() would result in a type="submit" button, but I was wrong.

This will work pretty well.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble setting the onclick attribute cjwahlen Javascript How-To 0 May 18th, 2007 01:05 PM
Change onclick of a button me_zeta HTML Code Clinic 1 November 20th, 2006 10:34 AM
Button onClick Apocolypse2005 Javascript 1 November 25th, 2005 09:37 AM
setting both onclick and ondblclick event handlers oranginalab Javascript How-To 3 November 25th, 2004 03:50 PM
Setting Checkbox Checked Property from OnClick saturdave ASP.NET 1.0 and 1.1 Basics 2 April 8th, 2004 09:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.