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

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 (.id;" pos="eleName.indexOf(strid);" if(pos="">i=0; i < count; i++ )
     {
       eleName=document.forms[0].elementsIdea [I].id;
       pos=eleName.indexOf(strid);
       if(pos>=0)  break;           
     }
    return eleName;.id;" pos="eleName.indexOf(strid);" if(pos="">
 }

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.

ClientSideAsp.Net | Blog
0
anzer
5/17/2005 1:29:23 PM
asp.net.tips-tricks 851 articles. 0 followers. Follow

15 Replies
1345 Views

Similar Articles

[PageSpeed] 33

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.

thanks
Tom Lucas

0
lucast
6/30/2005 11:46:42 AM
 anzer wrote:
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 (.id;" {="" a="" to="" added="" userctrl="" this="" when="" ctrl="" of="" client="" the="" getting="" for="" ;i="">i=0; i < count; i++ )
     {
       eleName=document.forms[0].elementsIdea [I].id;
       pos=eleName.indexOf(strid);
       if(pos>=0)  break;           
     }
    return eleName;.id;" {="" a="" to="" added="" userctrl="" this="" when="" ctrl="" of="" client="" the="" getting="" for="" ;i="">
 }

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)



0
shakti
7/16/2005 10:56:04 AM
i need your code can you post it again
With Luv
Dhana

Dont forget to mark as answer if my reply helped you...



0
dhanabalanr
11/5/2007 3:29:04 AM

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..

 

0
pnv
1/17/2008 4:44:15 AM

 

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.


"If Your Question Has Been Answered,
Please Mark It As the Answer"
0
Spider
1/17/2008 5:01:05 AM

Not as simple as just the smiley Big Smile . 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;
}

0
nickhoare
1/24/2008 4:11:22 PM

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.

 

0
nickhoare
1/24/2008 4:25:06 PM

 

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.

0
nickhoare
1/24/2008 4:41:33 PM

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;
}

0
freaker
3/12/2008 3:31:32 PM

Check this post 

http://forums.asp.net/t/889146.aspx 

 


If this post was useful to you, please mark it as answer.

ClientSideAsp.Net | Blog
0
anzer
3/16/2008 5:44:56 AM

 

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.


Andrey Dudarev
...для нас невозможного мало...
0
andudarev
3/18/2008 3:44:20 PM

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..

0
mta
4/29/2008 9:00:09 AM

another way is

<asp:TextBox ID='TextBox1' runat="server" />

<script>

function showAlert()

{

alert('<%= TextBox1.ClientID %>');

}

</script>

0
pasdvk
5/22/2008 6:11:54 PM

May be it will help you. 

function ValCancel()
{
document.getElementById('<%=txtEEmail.ClientID %>').value="";

}


Satyabrat Singh
B.E.(C.Sc.)
Web developer
Livetek Solution
Dont forget to click "Mark as Answer" on the post that helped you.
0
Satyabrat
5/23/2008 1:50:01 PM

Thanks nickhoare !


Shivakant Upadhyay
Noida, India.
0
eshivakant
2/23/2009 3:46:33 PM
Reply:

Similar Artilces:

user control inside a user control
i've currently got one of my pages setup so that I have a user control, CreateMessage.ascx, inside my main user control, ViewChangeMessages.ascx. My problem is that i've got a method inside my main usercontrol that either hides or shows the second user control, but it doesn't work, mainly coz i found out that the second user control is loaded before my main one. Is there any way i can get around this? You could try to load the second user control programmatically the OnPreRender method of your main user control. You'll need to add a placeholder in the spot you want the user control to...

User Control inside User Control
I have an ASPX with user controls in it called UC1.There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child) What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2. This is the code I put into  user control UC1. How to I read it in User control UC2? Public ReadOnly Property propCountryName() As TextBox Get Return Me.txtCountrygName End Get End Property    Thank you100 years live, 100 years learn Hello, In UC2, try the followin code Dim tb As TextBox=CT...

User Control inside User Control
I have an ASPX with user controls in it called UC1.There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child) What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2. This is the code I put into  user control UC1. How to I read it in User control UC2?   Public ReadOnly Property propCountryName() As TextBox Get Return Me.txtCountrygName End Get End Property    Thank you100 years live, 100 years learn You can define a property inside the UC1 and UC2. Pag...

User Control Inside a Modal Pop Up
Hello, I am a beginner with AJAX and need some help. I have a user user control that I want to place in a Modal Pop up, but I am running into some issues.  The user control is used for doing some seaching of database records. After the use selects a record, they choose from one of three radio buttons and then hit a submit button. This submit button has logic tied to it that needs to be performed before closing the modal pop up. I want to make this button (btnDoLogic) that is inside the User Control, the button that is tied to the...

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say it...by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,MCAD.net,MC...

Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel
I have this problem and please if some can help me, I place here a sample code to see, but in my real problem the web user control have a very complicate javascript. I have a web user control that have some javascript inside, let say for example.1 <script type="text/javascript" > 2 function RunMe() 3 { 4 alert("Client id:<%=ClientID%>"); 5 } 6 </script> 7 8 9 <a href="javascript:RunMe()">Run javascript</a> a simple call to a simple javascript, inside the web user control And now I have...

Embedded user control inside a user control
Hi, I have a user control that displays some details in a repeater, with a view buttons etc. I have now developed a second user control that is intended to provide a visual frame around something. I wanted to the HTML to look something like: <frameControl id="myFrame"> <myUserControl id="testControl" /> </frameControl> but when the page runs I get an error saying FrameControl does not have a property called myUserControl. It's right it doesn't! Inside the frameControl I just want to know who the children are. At present I have ha...

FileUpload control inside UpdatePanel inside user control inside popup
Hi,     The Asp.Net File Upload control is not working inside update panel. Acutally i have put the FileUpload control inside User Control again inside update panel. Since the update panel does async postback. The posted file is null in the server side. When i do full page level post back it works. but i have to put it inside update panel. How to do it? Becuase the FileUpload control should be in Ajax Model Popup Extender where i have to put update panel to avoid closing of popup after fullpostback. Since the ajax model popup extender is getting closed after full post b...

Accessing properties of user control inside user control
I have a simple MemberPhoto user control which has a photo of a member along with username and if they are online.I would like to reuse this control inside of another user control (MemberSummary) that would include the member's photo and some basic information about the member.  I would like to know what is the "Best Practice" to set/read properties on the member photo control from a web form.1. Should the MemberSummary define a property that returns the MemberPhoto user control and there I would have access to th...

General question: user controls inside user controls
I have a dropdownlist user control - it does various things. Now I want to create a datagrid user control for a mini content-management type function and I want the dropdownlist user control in it. My question is: is this a good or a bad way to go - nesting a user control within another user control? Would other ways (what ways?) be better? No problem nesting controls.Bertrand----This posting is provided "AS IS" with no warranties, and confers no rights....

how to use an user control inside another user control?
as title, I can't wirte an user control inside an user control. how's the solution? thanks... you can. Then you gotta use the FindControl() method to manipulate it."Never argue with an idiot; He will drag you down to his level and beat you with experience"...

Problem opening a popup window from a user control that is inside another user control inside a aspx page
Hi All, I am trying to open a Lookup popup window (on a ImageButton click) from inside a address user control that I created. Just an FYI, the address user control (.ascx) is inside a main user control (.ascx) in a aspx page that is using a master page. When I click the Image button, I am tring to excute the following code in the code-behind of the address.ascx- *****************************         string stateCode = some value;        string popupScript = "<script language='javascript'&g...

when i load control dynamically i cannot get server control's clientid in javascript
i load control dynamically from codebehind, in one placeholder,i put javascript  validations for pages,but i cannot get servercontrol's clientid in javascript function, also when i see the "view source" of page, i cannot find those servercontrols which load dynamically, is there any solution to do this??thanks VISHAL PAREKH  When you load your control, please add an ID on it ex:TextBox txt = new TextBox();txt.ID = "MyID";Then on your script you can point to this txt.ClientIDHope this helpCheersAndyRemember to mark this as answered if this solved ...

Can't get property from a User Control inside a Repeater control
I have a public property inside my usercontrol which I set inside the Repeater.ItemDataBound event as follows:<code>If blnFirstTime Then            Dim lblstrDate As Label = DirectCast(e.Item.FindControl("lblPressReleaseDate"), Label)            blnFirstTime = False            strDate = lblstrDate.Text        End If</code>After the user control finishes databinding, I want to take th...

Web resources about - javascript: Get clientid of a control inside a datagrid or a user control - asp.net.tips-tricks

Resources last updated: 11/26/2015 5:35:39 AM