Get 'real' ClientId

Posted on 2005-04-21
Last Modified: 2008-02-01
I'm trying to get the ClientId for a specific text box so I can use some JavaScript code to set the "focus" to this TextBox. I've got the JavaScript all figured out. Howver, I just can't get a 'real' ClientId value to pass to the JavaScript.

The text box is nested several controls deep... here is a rough hierarchy....

Page > User Control > Data Grid > Data Grid Footer Item > TextBox

Right now I try to get the ClientId for the TextBox but it never matches what is output to the browser. For example, my output is "txtStockNbrToAdd". However, what it needs to be is something like "myCart_dgCartList__ctl2_txtStockNbrToAdd".

I know I can get the ClientId of the "myCart" User Control. I also know I can get the ClientId of the "dgCartList" DataGrid. What I can't predict is the "_ctl2" portion.

There must be some way to get the information I'm looking for....

Question by:dtrick
    LVL 6

    Expert Comment

    At datagrid ItemDataBound, you can get the client ID for the textboxs inside datagrid using -  e.Item.FindControl("textbox_name").ClientID.ToString. This will return the textbox name that is assigned during the runtime datagrid__ctl3_textbox_name.

    Author Comment

    Actually, since the TextBox is in the footer I can't access it from the ItemDataBound event. However, I can and do access it from the ItemCreated event. I tried doing what you mention in the ItemCreated event and it still doesn't work. I'll include some source code... maybe that'll help.

    In the UserControl ********************
          <asp:Label id="lblStockNbr" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "STK_NBR") %>' />
          <asp:Label id="lblId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' Visible="False" />
          <asp:TextBox ID="txtStockNbrToAdd" Runat="server" CssClass="text" Width="80" />

    In the code behind ********************
    public void dgCartList_ItemCreated(object sender, DataGridItemEventArgs e)
          if (e.Item.ItemType == ListItemType.Footer)
                TextBox txtStockNbrToAdd = (TextBox)e.Item.FindControl("txtStockNbrToAdd");
                string strJS = "some js stuff here" + txtStockNbrToAdd.ClientID + "some js stuff here";
                Page.RegisterStartupScript("StockFocus", strJS);

    HTML source of text box in the DataGrid *******
    <input name="myCart:dgCartList:_ctl3:txtStockNbrToAdd" type="text" id="myCart_dgCartList__ctl3_txtStockNbrToAdd"  />

    HTML source of my JavaScript **************
    <script language="javascript">
          if (document.getElementById)
                x = document.getElementById(txtStockNbrToAdd);
    LVL 6

    Accepted Solution

    You should be able to access the textbox in ItemDataBound. The textbox gets the extra name "myCart_dgCartList__ctl3_" only in ItemDataBound and not during ItemCreated.

    Here is a code i tried in ItemDataBound and it worked

    TextBox txtStockNbrToAdd = (TextBox)e.Item.FindControl("txtStockNbrToAdd");
    string strJS = "<script language='javascript'>document.forms[0]." & txtStockNbrToAdd.ClientID & ".focus();</script>"
    Page.RegisterStartupScript("StockFocus", strJS);

    Author Comment

    You rock! That was it.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now