How do you get the ID of a control in ASP.NET

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2012-04-05T15:59:24Z Indexed on 2012/04/05 17:30 UTC
Read the original article Hit count: 164

Filed under:
|
|

I am attempting to loop through an array of numbers, match them to the checkbox they they are associated with, and checkmark that box.

My checkbox is set up like:

<input type="checkbox" runat="server" id="someID" name="somename" value="1234" />

My code when the page loads currently looks like:

foreach (string interest in interests_Var){
   foreach (var c in Page.Controls)
   {
   }
}

interests_Var is my array containing different numbers. We'll assume one of them is 1234. While looping through the page controls, I want to compare the value of the control to my number. If it equals my number, I want to then apply the attribute checked="checked". I'm assuming I have to find the ID of the control I am using, then use that ID to add a new attribute. Or is there a way I can add the attribute using the c variable?

I'm not dead set on this setup, so if you know a better way, I'm all ears. Thanks for any help and suggestions.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET