Asked by:
javascript: Get clientid of a control inside a datagrid or a user control

Question
-
User845932231 posted
Get clientid of a control inside a datagrid or a user control in client side so you can use this id in
document.getelementById( ) for some client side programming
function GetClientId(strid)
{
var count=document.forms[0].length;
var i=0;
var eleName;
for (<count ;i="" for="" getting="" the="" client="" id="" of="" ctrl="" when="" this="" userctrl="" added="" to="" a="" page="" {="" elename="document.forms[0].elements[i].id;" pos="eleName.indexOf(strid);" if(pos="">i=0; i < count; i++ )
{
</count> eleName=document.forms[0].elements[i].id;
pos=eleName.indexOf(strid);
if(pos>=0) break;
}
return eleName;<count ;i="" for="" getting="" the="" client="" id="" of="" ctrl="" when="" this="" userctrl="" added="" to="" a="" page="" {="" elename="document.forms[0].elements[i].id;" pos="eleName.indexOf(strid);" if(pos="">
}
</count>
Pass orginal server side id you given to the control as argument ( If you have a button btnSave in
you user control pass this to the function)
Tuesday, May 17, 2005 9:29 AM
All replies
-
User-1808527320 posted
Looks like the forum killed some syntax in your script. Care to try again. I think it would be great if we could only get it running.
thanksThursday, June 30, 2005 7:46 AM -
User-728729347 posted
Get clientid of a control inside a datagrid or a user control in client side so you can use this id in
document.getelementById( ) for some client side programming
function GetClientId(strid)
{
var count=document.forms[0].length;
var i=0;
var eleName;
for (<COUNT id="" page="" if(pos="" pos="eleName.indexOf(strid);" elename="document.forms[0].elements[i].id;" {="" a="" to="" added="" userctrl="" this="" when="" ctrl="" of="" client="" the="" getting="" for="" ;i="">i=0; i < count; i++ )
{
</COUNT>eleName=document.forms[0].elements[i].id;
pos=eleName.indexOf(strid);
if(pos>=0) break;
}
return eleName;<COUNT id="" page="" if(pos="" pos="eleName.indexOf(strid);" elename="document.forms[0].elements[i].id;" {="" a="" to="" added="" userctrl="" this="" when="" ctrl="" of="" client="" the="" getting="" for="" ;i="">
}
</COUNT>
Pass orginal server side id you given to the control as argument ( If you have a button btnSave in
you user control pass this to the function)
Saturday, July 16, 2005 6:56 AM -
User19442838 posted
i need your code can you post it againSunday, November 4, 2007 10:29 PM -
User-207142277 posted
Hi,
forum rich text box ruined you code. Can you please resend ur script. I am actully looking for a similar script like this.
Thanks..
Wednesday, January 16, 2008 11:44 PM -
User-122480877 posted
Hello to the people that like to bring this forum back from the dead.
If you used your mouse to go over the emoticaon ( [Idea] ) you will notice that it is a Barcket "[" followed bt "I" followed by closing Braket "]"
So all you need to do is replace [Idea] with [I]
Due to this happening all the time in the past they have changed the way emoticons work.
Hope that helps you out.
Thursday, January 17, 2008 12:01 AM -
User110737637 posted
Not as simple as just the smiley [:D] . I think the code should be as below. Not been able to check properly though.
function GetClientId(strid)
{
var count=document.forms[0].length;
var i=0;
var eleName;
for (i=0; i < count; i++ )
{
eleName=document.forms[0].elements[i].id;
pos=eleName.indexOf(strid);
if(pos>=0) break;
}
return eleName;
}Thursday, January 24, 2008 11:11 AM -
User110737637 posted
And since I found this post but it was of little help this tip might be useful for anyone else who passes.
If you make a user control and add an object with an id e.g. "MyTextBox", and you then insert the completed user control into a form calling the user control e.g. "MyControl" you will find that the textbox object created on the form gets the element name "MyControl_MyTextBox". That is every element of the user control has the name of that control together with an underscore Prefixed. So you can use that in your script.
Not yet found how to use the Get_ClientID function so that I do not have to make assumptions about the user control ID in the script.
Thursday, January 24, 2008 11:25 AM -
User110737637 posted
GetClientId("txt_Date")
then it returns "DateSelector1_txt_Date" (without quotes). So it does the job.
Thursday, January 24, 2008 11:41 AM -
User-1403527637 posted
This is a better one in my opinion as with the one we have here certain elements are not returned (html image elements, for example)
function GetClientId(strid)
{
var count=document.getElementsByTagName ('*').length; //<-- gets all elements, instead of Forms as this only returns FORM elements
var i=0;
var eleName;
for (i=0; i < count; i++ )
{
eleName=document.getElementsByTagName ('*')[i].id;
pos=eleName.indexOf(strid);
if(pos>=0) break;
}
return eleName;
}
Wednesday, March 12, 2008 11:31 AM -
Sunday, March 16, 2008 1:44 AM
-
User1677555125 posted
This code seems not to be working if you have controls inside GridView, Repeater etc.
For all rows the indexOf(ControlId) >= 0 will be true.
Tuesday, March 18, 2008 11:44 AM -
User1876771038 posted
I have a datagrid with embedded controls. The function works fine as long as there is only one row in the grid but im experiencing problems when grid has several rows.
The controlname returned will always be the one of the first row of the grid. Is there any way I can find out which row the wanted control is comming from?
If the question seems fuzzy please say so and I will try to elaborate.
tia..
Tuesday, April 29, 2008 5:00 AM -
User-1624839008 posted
another way is
<asp:TextBox ID='TextBox1' runat="server" />
<script>
function showAlert()
{
alert('<%= TextBox1.ClientID %>');
}
</script>
Thursday, May 22, 2008 2:11 PM -
User978097916 posted
May be it will help you.
function ValCancel()
{
document.getElementById('<%=txtEEmail.ClientID %>').value="";
}Friday, May 23, 2008 9:50 AM -
User400366069 posted
Thanks nickhoare !
Monday, February 23, 2009 10:46 AM -
User-1896916166 posted
this worked. Thanks
nothing like a good ol' O(n) linear search
Wednesday, June 3, 2009 5:47 PM