Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the Client ID of control within an ASP.NET GridView?

I have a asp:GridView which contains a asp:TextBox within a TemplateField. I would like to obtain it's ID for use in javascript. Something like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="textDateSent" runat="server" />
        <input type="button" value='Today' 
            onclick="setToday('<%# textDateSent.ClientID %>');" />
    </ItemTemplate>
</asp:TemplateField>

But when I compile, I get an error:

The name 'textDateSent' does not exist in the current context

Anybody know how to get the client ID of this TextBox?

like image 457
Keltex Avatar asked Jul 15 '09 00:07

Keltex


2 Answers

Try this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="textDateSent" runat="server">
        </asp:TextBox>                      
       <input type="button" value='Today' onclick="setToday('<%# ((GridViewRow)Container).FindControl("textDateSent").ClientID %>');" /> 
    </ItemTemplate>
</asp:TemplateField>
like image 52
Chris Mullins Avatar answered Sep 21 '22 14:09

Chris Mullins


Maybe you don't want to do it where you need the ClientID. Check out this post here where the controls in a row are referenced in a generic way.

like image 35
JBrooks Avatar answered Sep 21 '22 14:09

JBrooks