开发者

Accessing ClientID of a Control in a different TD

开发者 https://www.devze.com 2023-03-01 02:07 出处:网络
I am trying to access the ClientID of a control in onefrom a javascript (using jquery) call in a control in anotherin order to do some client-side show and hide effects. Here\'s the basic structure:

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 havi开发者_如何学Cng, 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.


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


To verify it can't find it, try this: alert($("#<%= blah.ClientID %>).length); If it returns a number greater than zero, then the issue isn't the find. Is the table row in a control like the <asp:Repeater /> control? If so, then you need to embed the script within the repeater template too.

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消