how to reference the id of my checkbox in a dynamic GridView control

brgdotnet
brgdotnet used Ask the Experts™
on
In my C# code I am creating a dynamic GridView control and one of the columns is a check box. In my C# code I am also assigning an ID to my
check box control in my RowDataBound event.  ===>   chkBox.ID = "CheckBox" + e.Row.RowIndex


However when my page is rendered, the id of the checkbox is prepended with the text "MainContent_myGrid_"  and appended by and underscore and then the number.
Example

<input id = "MainContent_myGrid_CheckBox3_3" type="checkBox" name="ctl00$MainContent$myGrid$ct103>


I want to be able to disable the checkbox using Java script.How can I reference the check box control by the ID I assigned to it in the GridView control?
Is there a way to do it, without having to reference the prepended "MainContent_myGrid_" text and the appended text of underscore followed by the number
of the check box?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Please refer to this for the detailed discussion on the subject. I think what you need is ClientId property.
Prakash SamariyaIT Professional
Top Expert 2015
Commented:
In your RowDataBound event, add css class to checkbox like below
chkBox.CssClass= "CheckBox" + e.Row.RowIndex 

Open in new window

So when it will render HTML, it will have attribute class with your given pattern like below
<input id = "MainContent_myGrid_CheckBox3_3" type="checkBox" name="ctl00$MainContent$myGrid$ct103 class="CheckBox3">

Open in new window

Now, you have to get checkbox using that class
var x = document.getElementsByClassName("CheckBox3");

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial