• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

Get 'real' ClientId

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

dtrick
0
dtrick
Asked:
dtrick
  • 2
  • 2
1 Solution
 
sansofturaCommented:
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.
0
 
dtrickAuthor Commented:
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 ********************
<ItemTemplate>
      <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" />
</ItemTemplate>
<FooterTemplate>
      <asp:TextBox ID="txtStockNbrToAdd" Runat="server" CssClass="text" Width="80" />
</FooterTemplate>

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);
            x.focus();
            alert('hi');
      }
</script>
0
 
sansofturaCommented:
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);
0
 
dtrickAuthor Commented:
You rock! That was it.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now