Hi all

I am trying to hide part of my form in ASP.NET when user clicks on check box. I want to implement this functionality on client side. I do not want to go back to Server.

Can someone help me with this one. I will appreciate your help.

Thanks & Regards
Masood

that calls for javascript or vbscript, but vbscript only works on IE.

So in javascript, add the following:

<script> 
function showhide(id){ 
if (document.getElementById){ 
obj = document.getElementById(id); 
if (obj.style.display == "none"){ 
obj.style.display = ""; 
} else { 
obj.style.display = "none"; 
} 
} 
} 
</script>

You call the code by putting it into an OnClick (or onClientClick if you're using a server control).

<a href="javascript:void(0)" onclick="showhide('divname')">More form.. lol</a>

Thanks for reply. Checkbox does not have onclick or onClientClick event when I look at properties window in VS2005.
Can someone clarify it or some working code sample will be greatly appreciated.

Thanks & Regards

that calls for javascript or vbscript, but vbscript only works on IE.

So in javascript, add the following:

<script> 
function showhide(id){ 
if (document.getElementById){ 
obj = document.getElementById(id); 
if (obj.style.display == "none"){ 
obj.style.display = ""; 
} else { 
obj.style.display = "none"; 
} 
} 
} 
</script>

You call the code by putting it into an OnClick (or onClientClick if you're using a server control).

<a href="javascript:void(0)" onclick="showhide('divname')">More form.. lol</a>

it may not show in intellisense, but it is a working attribute among all browsers.

Try the code sir.

<input type="checkbox" id="chkBox" onclick="showhide('divname')" value="blah" />

But if you're using it with a checkbox, check to see if the checkbox is checked first.

function showhide(id)
{
  if (document.getElementById)
  {
    obj = document.getElementById(id);
    if (obj.checked)
    {
      if (obj.style.display == "none")
      { 
        obj.style.display = "block"; 
      }
    } else { 
      obj.style.display = "none"; 
    }
  }
}

Hi buddy

Thanks very much for your help. Now it is working.
I still need to work out onClientClick property for Server Control.
Anyway I am happy at least it is working with HTML control.
If you have any example for Server Side CheckBox control please post it. It will be great.

Thanks again.

You can use it, even though it's not an option.

<asp:CheckBox ID="chkBox" OnClientClick="showhide('divname')" ..... runat="server" />

Now if for some reason that doesn't work, then add the attribute at runtime:

<asp:CheckBox ID="chkBox" ..... runat="server" />

Sub Page_Load
chkBox.Attributes.Add("OnClick","showhide('divname')")
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.