Groups > Asp .Net > ASP.NET Tips and tricks > Re: javascript: Get clientid of a control inside a datagrid or a user control




Re: javascript: Get clientid of a control inside a datagrid
or a user control

Re: javascript: Get clientid of a control inside a datagrid or a user control
Thu, 24 Jan 2008 16:11:22 +000
Not as simple as just the smiley  . 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;
}
Post Reply
Re: javascript: Get clientid of a control inside a datagrid or a user control
Thu, 24 Jan 2008 16:25:06 +000
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.
Post Reply
Re: javascript: Get clientid of a control inside a datagrid or a user control
Thu, 24 Jan 2008 16:41:33 +000
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.
Post Reply
Re: javascript: Get clientid of a control inside a datagrid or a user control
Wed, 12 Mar 2008 15:31:32 +000
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;
}
Post Reply
Re: javascript: Get clientid of a control inside a datagrid or a user control
Sun, 16 Mar 2008 08:44:56 +000
Check this post 

http://forums.asp.net/t/889146.aspx
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact