Avatar of QPR
QPRFlag for New Zealand

asked on 

onClientClick not happening

Not sure if I've got the JS wrong or the .net code wrong but....
I have an asp button and I added the property onclientclick, I'm trying to set a DIV to visible when the button is clicked. The page refreshes but the DIV is still hidden any ideas?

<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
//if(e.style.display == 'none')
e.style.display = 'block';
//else
//e.style.display = 'none';
}
//-->
</script>

<asp:Button ID="AddMe" runat="server" Text="Add" OnClientClick="toggle_visibility('NewAbs')" />

<div id="NewAbs" style="visibility: hidden"> <table cellpadding="4" cellspacing="4" border="1">
some stuff in here
</table>
 </div>
ASP.NET

Avatar of undefined
Last Comment
TimCottee
Avatar of strickdd
strickdd
Flag of United States of America image

Make sure your javascript is being called like so:

function toggle_visibility(id) {

alert("Hello");

}

if that works, there is another problem
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to make sure that the button's click event which posts back the page doesn't get fired. Adding return false; to the end of the onclientclick will achieve this:


<asp:Button ID="AddMe" runat="server" Text="Add" OnClientClick="toggle_visibility('NewAbs');return false;" />

Avatar of QPR
QPR
Flag of New Zealand image

ASKER

"<asp:Button ID="AddMe" runat="server" Text="Add" OnClientClick="toggle_visibility('NewAbs');return false;" />"

Does nothing, no postback/refresh/showing of DIV
Avatar of QPR
QPR
Flag of New Zealand image

ASKER

"function toggle_visibility(id) {

alert("Hello");}"

this works, so what is wrong with my initial code (in first post)
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland 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
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