Search code examples
jqueryasp.netclientid

Accessing ClientID of a Control in a different TD


I am trying to access the ClientID of a control in one from a javascript (using jquery) call in a control in another in order to do some client-side show and hide effects. Here's the basic structure:

<td>
  <asp:LinkButton OnClientClick="DoStuff" runat="server">
</td>
<td>
  <asp:TextBox ID="blah" runat="server">
</td>

In Scripts:

function DoStuff() {
  $("#<%= blah.ClientID %>").hide();
}

The problem I'm having, is that the function does nothing. It works fine hiding objects in the same TD, but here it doesn't seem to be able to find the control.


Solution

  • If you're referring directly to the control on the server side, ClientID should including any NamingContainer information (from something like a GridView or a Repeater). Find out what the client ID of the control is using your browser's developer tools (IE has Developer Tools, Firebug for Firefox, etc.), then debug JavaScript (with the same tool) and do a $("#theidyoujustfound") in the JavaScript console and see what you end up with.

    It also looks like there's a missing double quote in your DoStuff() method; you'll want $("#<%= blah.ClientID %>").hide();. And I'm not sure it's necessary, but it couldn't hurt to include the parenthesis for the OnClientClient function:

    <asp:LinkButton OnClientClick="DoStuff()" runat="server">