How do i hide a table row in asp.net web application

i am hiding a row of a table using javascript function in asp.net page .here, i m using a check box , when i ll checked the checkbox , it will hide the row , and when i ll un checked it will show the row, but i m getting problem when i am submitting the data by onclick button event the row is showing.means
 when the checkbox is checked means it will hide the row.but when i m submitting the data on Onclick event of the button the row is visible,means when the page is postback that time the row is visible.so i m getting problem.plz help me urgently.

<body>
<input id="Checkbox1" onClick="toggle()" type="checkbox" />
    
<table border="1">
<tr>
<td>Always visible</td>
</tr>
<tr id="hidethis" runat="server">
<td>Hide this</td>
    
</tr>
<tr>
<td>Always visible</td>
</tr>
</table>   
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
</body>
</html>
<script language="javascript" type="text/jscript">
    function toggle() 
{
 if( document.getElementById("hidethis").style.display=='none' )
{
  document.getElementById("hidethis").style.display = 'block';
  return true;
 }else{
  document.getElementById("hidethis").style.display = 'none';
  return false;
 }
}
    
    
    </script>

Open in new window

tanaya80Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
-sg-Connect With a Mentor Commented:
Ok, cool.  Just put the script block at the bottom of the html code of the control like I have attached.

If you find that it is still not working, check the page source from the browser and see if asp.net has renamed the client ID's.  It will do this if using master pages for example.  If that's the case, then simply replace

getElementById("Checkbox1")

with

getElementById("<%=this.Checkbox1.ClientID%>")

assuming you are using C#
    <script type="text/javascript">
        document.getElementById("hidethis").style.display = ((document.getElementById("Checkbox1").checked)?'block':'none');
    </script>

Open in new window

0
 
-sg-Commented:
Here is the modified code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
      function toggle() 
      {
        document.getElementById("hidethis").style.display = ((document.getElementById("Checkbox1").checked)?'block':'none');
      }
    </script>
</head>
<body onload="toggle()">
<form runat="server" id="form1">
<asp:CheckBox runat="server" id="Checkbox1" onClick="toggle()" EnableViewState="True" checked="True" />
    
<table border="1">
<tr>
<td>Always visible</td>
</tr>
<tr id="hidethis" runat="server">
<td>Hide this</td>
    
</tr>
<tr>
<td>Always visible</td>
</tr>
</table>   
<asp:Button ID="Button1" runat="server" Text="Button" OnClick=Button1_Click  />
</form>
</body>
</html>

Open in new window

0
 
tanaya80Author Commented:
ok this is working fine in page level, but when i m using in .acx page means in a webcontrol or in a gridview then it is not working bcz how i will define the <body onload="to toggle()"> , is there any alternative solution.




0
All Courses

From novice to tech pro — start learning today.