Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please Help me It's Urgent

I am
Making a dotnet page where i am take 4 html checkbox and onclick of every checkbox i display and hide a div box. i am make a function check1 and call the html checkbox. But when i want to this checkbox on aspx.cs C# page for coding so i am adding runat="server" attribute in every html checkbox. After that javascript gives error on click of checkbox is object expected. Please help me.

code is below: --
    function check1() {
			alert("hi");
//        alert(document.getElementById('add_acad'));
        var checkbox1 = document.getElementById('add_acad');
        var panel1=document.getElementById('academicpanel');
        if (checkbox1.checked)
            panel1.style.display = 'block';
        else
            panel1.style.display = 'none';
        var checkbox2 = document.getElementById('add_grade');
        var panel2 = document.getElementById('gradepanel');
        if (checkbox2.checked) {
            panel2.style.display = 'block';
        }
        else
            panel2.style.display = 'none';
        var checkbox3 = document.getElementById('add_health');
        var panel3 = document.getElementById('healthpanel');
        if (checkbox3.checked) {
            panel3.style.display = 'block';
        }
        else
            panel3.style.display = 'none';
        var checkbox4 = document.getElementById('addother');
        var panel4 = document.getElementById('leavepanel');
        if (checkbox4.checked)
            panel4.style.display = 'block';
        else
            panel4.style.display = 'none';
    }


 <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td><label>
            <input type="checkbox"  runat="server" name="add_acad" id="add_acad" checked="checked"  önclick="check1()"/>
          Add Academic Record</label></td>
          <td><label>
            <input type="checkbox"  runat="server" name="add_grade" id="add_grade"   önclick="check1()"/>
          Add Grade</label></td>
          <td><label>
            <input type="checkbox"  runat="server" name="add_health" id="add_health"   önclick="check1()"/>
          Add Health Record</label></td>
          <td><label>
            <input type="checkbox"  runat="server" name="addother" id="addother"   önclick="check1()"/>
          Add Other Record</label></td>
        </tr>
      </table>
Posted
Updated 21-Oct-11 8:48am
v2

ASP.NET addes a prefix to controls, something like "ctl00_PlaceHolderArea", so when trying to access the is JavaScript as you are...

document.getElementById('add_acad');


...the control no longer has the same name. It would now be something like this

document.getElementById('ctl00_PlaceHolderArea_add_acad');


You should look into using JQuery. You would reference the control as

$("[id$='add_acad']")


This will find any control with an id that contains "add_acad" no matter where on the page it is or what the prefix is.
 
Share this answer
 
You need to use Obj.ClientID like this:
JavaScript
document.getElementById('<%= add_acad.ClientID%>');

Just be aware that it doesn't work in js files.
 
Share this answer
 
v2
Comments
[no name] 21-Oct-11 15:25pm    
This will only work if the JavaScript is embedded within the aspx markup, which is not a good design choice IMO
aidin Tajadod 21-Oct-11 16:51pm    
I have mentioned that: it doesn't work in JS files!
Manfred Rudolf Bihy 21-Oct-11 15:46pm    
That was what I wanted to say! :-)
aidin Tajadod 21-Oct-11 16:52pm    
I agree, It makes the aspx files look ugly! So what is your suggestion?
[no name] 21-Oct-11 17:53pm    
"So what is your suggestion?" See solution 1 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900