How can I access child controls of a web form?
-
I have a web page with a asp:checklistbox object. I have buttons to "check all" checkboxes; however, to avoid the roundtrip back to the server, I was wondering if I could use jscript to set the checkboxes in the client. The problem I have is how do I access all of the checkboxes that get automatically generated by the checklistbox? If I can access all the checklistboxes of the form then I could set their value to checked.
-
I have a web page with a asp:checklistbox object. I have buttons to "check all" checkboxes; however, to avoid the roundtrip back to the server, I was wondering if I could use jscript to set the checkboxes in the client. The problem I have is how do I access all of the checkboxes that get automatically generated by the checklistbox? If I can access all the checklistboxes of the form then I could set their value to checked.
I can't remember exactly how I did this, but I can tell you the steps to discover it. * Look at the output of the webpage - That is, look at source that you get in the browser. * Examine the IDs of the checkboxes in the list and see how they compare to the name you gave the asp:checklistbox (IIRC they will have something appended to the name, and perhaps something prefixed if it is contained in another control) * Write some javascript to use the pattern you have discovered.
My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
-
I can't remember exactly how I did this, but I can tell you the steps to discover it. * Look at the output of the webpage - That is, look at source that you get in the browser. * Examine the IDs of the checkboxes in the list and see how they compare to the name you gave the asp:checklistbox (IIRC they will have something appended to the name, and perhaps something prefixed if it is contained in another control) * Write some javascript to use the pattern you have discovered.
My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
Colin, Thanks for your input. You guided me into the right direction. Following is the solution in case anyone needs it as well:
<script language=vbscript> option explicit <!---------------- check/clear text boxes -------------> dim currChkboxName dim currCheckValue sub UpdateCheckboxes(chkboxName, checkValue) currChkboxName = chkboxName currCheckValue = checkValue dim i for i = 0 to Form1.children.length-1 UpdateChildCheckboxes Form1.children(i) next end sub sub UpdateChildCheckboxes(obj) dim i if not obj is nothing then if obj.id <> "" then if InStr(1, obj.id, currChkboxName) > 0 then obj.checked = currCheckValue end if end if for i = 0 to obj.children.length UpdateChildCheckboxes obj.children(i) next end if end sub </script>
Then declare the buttons as follows:<input id="btnClearTestConditions" type=button value="Clear All" onClick="UpdateCheckboxes 'cklTestConditions_', false" /> <input id="btnCheckAllTestConditions" type=button value="Check All" onClick="UpdateCheckboxes 'cklTestConditions_', true" >