0

How can I find dynamically generated Html Checkboxes from C#. Need to find them by id and mark them as checked.

This code generates the HTML first:

StringBuilder sbHtml = new StringBuilder("");
sbHtml.Append("<div class=\"checkboxBtn\">");
sbHtml.Append("<input type=\"checkbox\" runat=\"server\" 
                class=\"chkBx\" id=\"" + 
                Convert.ToString(someid) + "\" />");
sbHtml.Append("<label>Compare</label>");                    
sbHtml.Append("</div>");

and the rendered HTML is

<div class="checkboxBtn">
 <span class="uncheked"></span>
 <input type="checkbox" runat="server" class="chkBx" id="23"></input>
 <label>Compare</label>
</div>

There are many such checkboxes and I would like to find them by IDs

string[] PhoneIds = {"11","23","43"};
foreach(string id in PhoneIds)
  {
    HtmlInputCheckBox cBox = form1.FindControl(id) as HtmlInputCheckBox;
    if(cBox!=null)
     {
       //cb.checked = true;
     }
  }

The if condition always fails as if the checkboxes dont exist. What should be done here.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
sukesh
  • 2,379
  • 12
  • 56
  • 111
  • Just out of curiosity, why aren't you just creating asp checkboxes?(not wrong just asking) – Koen Nov 18 '14 at 11:16
  • those are css styles checkboxes & for some reason only working with html and not asp. Tried asp too – sukesh Nov 18 '14 at 11:18
  • ow weird, cause if i create them, then do Checkbox.id = "chck" than i can change style with css – Koen Nov 18 '14 at 11:20
  • So you are building this string of html - fine. How and when do you then add this to the form so that they are available as controls in the code-behind? Hard to say without the rest of the code, but it looks to me like you would be better off with a [`Repeater`](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(v=vs.110).aspx) and check the checkbox in an [`ItemDataBound`](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx) event. – Rhumborl Nov 18 '14 at 11:45

3 Answers3

2

you are assigning only one number to your string-arrey PhoneIds:

string[] PhoneIds = ["11,23,43"];

instead you need to use:

string[] PhoneIds = {"11","23","43"};

Then you will find them.

Because otherwise you would assign one string to your array, and thisone would be 11,23,43 and as far as you have no checkbox with that string, the if-condition fails.

Also you should not use numbers as ID's, as you can check out in this answer, where it states that until HTML5

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

you must start with a letter. Maybe your browser doesn't support HTML5 ?

Community
  • 1
  • 1
DatRid
  • 1,169
  • 2
  • 21
  • 46
  • sorry that was a typo. I get the IDs fine. not that issue – sukesh Nov 18 '14 at 11:20
  • @Qwerty Oh okay. Are you using a master-page? And which version of asp.net? Also in which state of the lifecycle do you want to find the checkboxes? – DatRid Nov 18 '14 at 11:24
  • This is a usercontrol. during postback. asp4.0 – sukesh Nov 18 '14 at 11:30
  • Could you please add a bit more info about your usercontrol? Its hard to understand for me what you do there, do you want to insert the html into a usercontrol and then you reuse the usercontrol somewhere else and you want to find the checkboxes ? Please provide a bit more info. – DatRid Nov 18 '14 at 11:47
1

Element IDs in HTML must not start with a number. That may be why ASP.NET can't find it either. I would put a prefix on it, such as cbox11.

To fix the HTML:

StringBuilder sbHtml = new StringBuilder("");
sbHtml.Append("<div class=\"checkboxBtn\">");
sbHtml.Append("<input type=\"checkbox\" runat=\"server\" 
                class=\"chkBx\" id=\"cbox" + 
                Convert.ToString(someid) + "\" />");
sbHtml.Append("<label>Compare</label>");                    
sbHtml.Append("</div>");

And for the loop

string[] PhoneIds = {"11", "23", "43"};
foreach(string id in PhoneIds)
{
    HtmlInputCheckBox cBox = form1.FindControl("cbox" + id) as HtmlInputCheckBox;
    if(cBox!=null)
    {
        //cb.checked = true;
    }
}
Rhumborl
  • 16,349
  • 4
  • 39
  • 45
0

You don't need to check these in the code behind, it can be done in jQuery:

<div class="checkboxBtn">
    <span class="uncheked"></span>
    <input type="checkbox" runat="server" class="chkBx" id="23"></input>
    <label>Compare</label>
</div>

<script type="text/javascript">
   $(function(){
        $('div.checkboxBtn input[type="checkbox"]').prop('checked', true);
   });
</script>

This will check all child checkboxes of div.checkboxBtn.

DGibbs
  • 14,316
  • 7
  • 44
  • 83