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 (i=0; i < count; i++ )
{
eleName=document.forms[0].elements[i].id;
pos=eleName.indexOf(strid);
if(pos>=0) break;
}
return eleName;
}
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)
If this post was useful to you, please mark it as answer.
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 (i=0; i < count; i++ )
{
eleName=document.forms[0].elements[i].id;
pos=eleName.indexOf(strid);
if(pos>=0) break;
}
return eleName;
}
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)
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;
}
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.
OK it works. I have a user control which contains a text box with an id of "txt_Date". I added the control to a form and the user control has the id "DateSelector1". So on the web form the text box gets the id "DateSelector1_txt_Date". If I call the Get_ClientId
function like this
GetClientId("txt_Date")
then it returns "DateSelector1_txt_Date" (without quotes). So it does the job.
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;
}
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.
Contributor
4387 Points
1407 Posts
javascript: Get clientid of a control inside a datagrid or a user control
May 17, 2005 09:29 AM|anzer|LINK
document.getelementById( ) for some client side programming
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;
}
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)
ClientSideAsp.Net | Blog
None
0 Points
932 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jun 30, 2005 07:46 AM|lucast|LINK
thanks
None
0 Points
1 Post
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jul 16, 2005 06:56 AM|shakti|LINK
Member
622 Points
180 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Nov 04, 2007 10:29 PM|dhanabalanr|LINK
Dhana
Dont forget to mark as answer if my reply helped you...
Member
711 Points
224 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jan 16, 2008 11:44 PM|pnv.ravikiran|LINK
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..
Happy coding..
-------------------------------------------------------------------
Please mark as answer if you find this post helpful.
Participant
845 Points
467 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jan 17, 2008 12:01 AM|Spider Master|LINK
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.
Trading Center is a Continuation of the Classifieds Starter Kit onCode Plex.
None
0 Points
9 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jan 24, 2008 11:11 AM|nickhoare|LINK
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;
}
None
0 Points
9 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jan 24, 2008 11:25 AM|nickhoare|LINK
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.
None
0 Points
9 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jan 24, 2008 11:41 AM|nickhoare|LINK
GetClientId("txt_Date")
then it returns "DateSelector1_txt_Date" (without quotes). So it does the job.
None
0 Points
2 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Mar 12, 2008 11:31 AM|freaker|LINK
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;
}
javascript getClientId
Contributor
4387 Points
1407 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Mar 16, 2008 01:44 AM|anzer|LINK
Check this post
http://forums.asp.net/t/889146.aspx
ClientSideAsp.Net | Blog
Member
580 Points
180 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Mar 18, 2008 11:44 AM|andudarev|LINK
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.
...для нас невозможного мало...
None
0 Points
1 Post
Re: javascript: Get clientid of a control inside a datagrid or a user control
Apr 29, 2008 05:00 AM|mta|LINK
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..
None
0 Points
1 Post
Re: javascript: Get clientid of a control inside a datagrid or a user control
May 22, 2008 02:11 PM|pasdvk|LINK
another way is
<asp:TextBox ID='TextBox1' runat="server" />
<script>
function showAlert()
{
alert('<%= TextBox1.ClientID %>');
}
</script>
Member
188 Points
107 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
May 23, 2008 09:50 AM|Satyabrat|LINK
May be it will help you.
function ValCancel()
{
document.getElementById('<%=txtEEmail.ClientID %>').value="";
}
B.E.(C.Sc.)
Web developer
Livetek Solution
Dont forget to click "Mark as Answer" on the post that helped you.
Member
110 Points
33 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Feb 23, 2009 10:46 AM|eshivakant|LINK
Thanks nickhoare !
Noida, India.
None
0 Points
9 Posts
Re: javascript: Get clientid of a control inside a datagrid or a user control
Jun 03, 2009 05:47 PM|alejjak|LINK
this worked. Thanks
nothing like a good ol' O(n) linear search