Getting a ref (ID's or Names) for Dynamic Controls on a Page

 I have a page containing a number of dynamically created checkboxes within a gridview (1 checkbox per row). When building the gridview  I can assign an 'onclick' event handler to the control so that a client event is triggered.

However I need the name/ID of the checkbox control that the user clicked on it. Is there any way to do this via Javascript/client side code?

Any examples/sample code would be helpful.






11/19/2007 10:47:15 PM 24353 articles. 1 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 56


 Eric | Twitter epascarello | LinkedIn
11/19/2007 11:19:40 PM

No - this will not provide access to the control name - it's a bit more complex than that with dynamic controls. I need to get the name into a string variable.

Remember that the control  (in this case an unbound checkbox) is getting created dynamically in a rowdatabound event of a gridview as follows:

    e.row.cells[0].Addtibutes.Add ("onclick", "somejavascriptfunction()"); 

The ID for the control does not seem to be accessible here. It's also unclear how to get a ref to the name of the control so I can parse it out in client side javascript.

 Any other ideas?


11/19/2007 11:55:09 PM

You are adding the onclick onto the cell and not the control in the cell so of course you will not get the id of the checkbox.

You can either figure out how to add the onclick to the checkbox or If that onclick fires you can do


Eric | Twitter epascarello | LinkedIn
11/20/2007 1:12:54 AM

i have a problem with dynamically generated check boxes inside a datagrid, in a modal window. it is not identifying if the check box is checked or not(in certain machines). the application is in ASP .net 2.0

when i checked the view source of this modal window i am able to fine a difference in the view source for the same control in different machines as follows.

*(error) (1) <TD><INPUT id=SearchTDS_UA1_dgSearchResults_ctl04_chkDelete type=checkbox name=SearchTDS_UA1$dgSearchResults$ctl04$chkDelete></TD>

( 2) <TD><INPUT id=SearchTDS_UA1_dgSearchResults__ctl4_chkDelete type=checkbox name=SearchTDS_UA1:dgSearchResults:_ctl4:chkDelete> </TD>

the 2nd is working fine . but the 1st one is giving an error.

I am not able ot fix thie error as i dont knwo how the id is generated for a dynamic control.

please help as soon as possible.

10/31/2008 6:40:08 AM

Similar Artilces:

how to get a control ID from a bind field name / bound field (column) name from a formview's control
I have a form view which is bound to a table, now from codebehind file I need to apply some formatting based on some of the field's bound control. For example, if Formview1 is bound to Employee table, and it's item template has a control with ID "Label1" which is bound to "EmployeeJoinDate" . Now, I have a different table which has a list of field name(EmployeeJoinDate is one of them), all of which should be applied some formatting based on some condition. Now I can take two approach for that, but I am stuck at both:  1. If I can somehow get the contro...

Open file in browser or application getting the page's name as the file's name
Hi, I'm am trying to get a file to open in either a browser or application depending on the mime type. I'am using the following code in the page load. protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack)    { DocumentInfo document = (DocumentInfo)Session["View_Document"]; StreamFile(document.Document, document.DocumentName, document.FileType.MimeType); } } }private void StreamFile(byte[] fileData, string originalFileName, string mimeType) { try { Response.Clear(); Response.ContentType = mimeType; Response.AddHeader("Con...

Can I hide a user control's control from the hosting page's client script?
Okay, my previous question was not unswered and I guess you can't hide a control's panel with server code then show it with client code. Now this is what I try to do: on the hosting page's onload event, call a function to hide the user control's penal. Here is the deal: the user control was looped as many times as data required. So the penals will be name mypanel1, my panel2, etc... that is not even a problem if I know if the following is true: * Do all server codes finish running before the page get sent back to the client? Say the database gave me 5 penals, I can then determine how...

Getting the recreated control's id's by viewstate?
Hi How can i get the automaticly created fileupload control's id's by stroring them in the viewstate? Is it possible? or any other way to get them on the run-time? I can store button's click number and then create dynamic controls on the run-time and if i look at the source code i can see the auto-created id's but when i click submit button to upload files in fileupload controls i get an NullReferenceException. I guess when i click submit button all the id's gone because the page posts back. So i write this to read id's and assign to my fileupload control, here is the code for it...

How to get browser's name client side?
Is there any object/method that will give me information on the browser, client side? For example, I need to detect IE browser. I am using:  function st_IsIE() { if(navigator.appName.indexOf("Microsoft Internet Explorer") != -1) { return true; } return false; }   Is there a better way?   Hi, Check this, one of the best script i found for doing this - you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum...

Treeview control: how do you access client id's for each node's checkbox?
Is it possible to get the client id's for the checkboxes that are created by a treeview control?  I need to create some javascript that will manipulate the checked status of the boxes on the client side, but can not figure out how to determine the client id's for each checkbox. I am in a situation where the tree can have 4 levels of nodes, and they are not populated unless the user expands a node.  This causes the client id's to constantly change as they are numerically numbered from the top down. Thanks, Billy You can get the ClientID of a checkbox inside javascript as follow...

trouble getting id's from dynamically created controls...
I have a webform that dynamically creates several RadioButtonList controls and places then on a panel control. To the side of each RadioButtonList control is a LinkButtonControl. What I am trying to day is reset the RadioButtonList control when the user clicks on the respective LinkButtonControl. // PARTIAL CODE LISTING... // create radiobuttonlist and add to panel control RadioButtonList rlbAnswer = new RadioButtonList(); rlbAnswer.ID = String.Format("rblAnswer{0}", drGetQuestionList.GetInt32(2)); rlbAnswer.RepeatDirection = RepeatDirection.Horizontal; ...

Client-side Dynamic Modification of a ModalPopup's Target Control?
I have a requirement to use ModalPopup type functionality to display a data entry form and, after submission, display a thank you screen. No postbacks should be involved, and the form / screen transition should be seamless. I've done a simple prototype of this using an UpdatePanel and partial rendering, but I'm not 100% happy with it since the URL in the web logs for the partial postbacks would be different for every page from which this form is submitted (the ModalPopup appears from a link that's on every page, via a master page).I'm wondering if the target control of a Moda...

can not get control's client id in js file
hi i have a search control in my pr. and i create a js file to check weather the textbox is blank or not and other functions. now my problem is if ($get('<%=txtdoctorsearch.ClientID%>').value == '') {alert('Please Enter Name or City or State or Country'); $get('<%=txtdoctorsearch.ClientID%>').focus();return false; }if ($get('<%=ddlspecialization.ClientID%>').selectedIndex == '0') {alert('Please Enter Specialization'); $get('<%=ddlspecialization.ClientID%>').focus();return false...

how to get page's width and page's height
hi all....... i created a user control function in .ascx file which contain a button, it display a message in <div>, and i include this user control file into many web pages, but the div display's position is vary because the height in each page is different... so how can i get the page's height of the current display page.... thanks use javascript to receive window size, this function will display it with alert: function alertSize() {  var myWidth = 0, myHeight = 0;  if( typeof( window.innerWidth ) == 'number' ) {    //Non-IE    myWidth = ...

Naming a document element problems (trying to getElementByID() but the id's of the control are staring with _'s, which are not valid)
This page is an almost working implementation of viewing the HTML source, you'll see the two files are very close, with the difference being that the spans in the working on faulty version are named "_ctl0_Strategy0" instead of "Strategy0". That causes the getElementByID() javascript function to fail because id's staring with _'s are not valid). Since as far as I know, I can't control the control's naming prefix (via a C# command), how do I get around this?Mark You could just append the _ctl0_ in your JavaScript.Are you making this ...

problem with dynamically loaded user control's name and id being altered
Hello, this is my first post on this forum so go easy on me Say I have the following container control (red)in my dafault.aspx page <form id="form1" runat="server"> <div> <div id="ContentZone" runat="server"/> </div> </form> When I dynamically add a control to the ContentZone, the name and id of that new control is unchanged code in the codefile for dafault.aspx  Dim a As TextBox a = New TextBox() a.Text = "test" a.ID = "testLabel" ContentZone.Controls.Add(a)  The HTML output will be <div id="ContentZone"...

Master page id's and rendered id's not matching up
Hi all, I've got a question... I'm pretty new to masterpages and have a simple one setup that looks like this.<html xmlns="" ><head runat="server">    <title>Simple test</title></head><body>    <form id="form1" runat="server">    <strong>form</strong>    </form></body></html>however when I view the rendered source code the form line looks like this<form name="aspnetForm"...

Get Server-Side's control with $get
Hi, I am a novice at Ajax.NET If I have button with ID="Btn1", how could I use $get('') or take any other measure to retrieve the ref to the button at client-side? Cheers for any help. Ricky.Take nothing on looks; take everything on evidence.- Charles Dickens Try this link: $get('Btn1') Encosia - ASP.NET, AJAX, and more.Latest article: Using complex types to make calling services less… complex Hi, if the server-side Button with Button.ID="Btn1" is insid...

Web resources about - Getting a ref (ID's or Names) for Dynamic Controls on a Page -

Resources last updated: 12/28/2015 10:08:24 PM