如何使用javascript和/或服务器端标记获取绑定控件的客户端ID?

时间:2022-03-09 16:56:53
<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments" onChange="javascript:checkLength(<%# tbComments.ClientId %>);" runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>

Any ideas to make the above working (which doesn't :P)?

任何使上述工作的想法(不是:P)?

4 个解决方案

#1


Change your markup to pass "this" as a reference to the given comments box.

更改标记以将“this”作为对给定注释框的引用。

<asp:TextBox ID="tbComments" 
           onChange="javascript:checkLength(this);" runat="server"/>

Then in your checkLength() function, "e" is a direct reference to the DOM element that raised the onchange event.

然后在checkLength()函数中,“e”是对引发onchange事件的DOM元素的直接引用。

function checkLength(e){
  alert(e.id); //id of the comments box
  //get a reference to the span element immediately after the textbox
  theSpan = e.parentNode.getElementsByTagName("span")[0];
  theSpan.innerHTML = "comments length: " + e.value.length;
}

#2


Just add a class to the span or the textbox and use jQuery to find the element in the dom and add a change event to the textbox run the checkLength code

只需在span或文本框中添加一个类,然后使用jQuery在dom中查找元素,并在文本框中添加change事件,运行checkLength代码

$(document).ready(function()
{
    $('.myClass').change(function()
    {
         // do your length check...
    });
});

#3


Are you sure the problem isn't just the mispelling of "ClientId" in the following tag? :-)

您确定问题不仅仅是在以下标签中拼写错误的“ClientId”吗? :-)

<span id="<%# tbComments.CliendId %>Label"></span>

#4


Is there something wrong with your JavaScript checkLength() function not appending Label to the Client ID? Then have your Javascript take both?:

您的JavaScript checkLength()函数是否有错误将Label附加到客户端ID?然后让你的Javascript同时使用?:

<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments"
            onChange="javascript:checkLength(<%# tbComments.ClientId %>, <%# tbComments.ClientId %>Label);"
            runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>

#1


Change your markup to pass "this" as a reference to the given comments box.

更改标记以将“this”作为对给定注释框的引用。

<asp:TextBox ID="tbComments" 
           onChange="javascript:checkLength(this);" runat="server"/>

Then in your checkLength() function, "e" is a direct reference to the DOM element that raised the onchange event.

然后在checkLength()函数中,“e”是对引发onchange事件的DOM元素的直接引用。

function checkLength(e){
  alert(e.id); //id of the comments box
  //get a reference to the span element immediately after the textbox
  theSpan = e.parentNode.getElementsByTagName("span")[0];
  theSpan.innerHTML = "comments length: " + e.value.length;
}

#2


Just add a class to the span or the textbox and use jQuery to find the element in the dom and add a change event to the textbox run the checkLength code

只需在span或文本框中添加一个类,然后使用jQuery在dom中查找元素,并在文本框中添加change事件,运行checkLength代码

$(document).ready(function()
{
    $('.myClass').change(function()
    {
         // do your length check...
    });
});

#3


Are you sure the problem isn't just the mispelling of "ClientId" in the following tag? :-)

您确定问题不仅仅是在以下标签中拼写错误的“ClientId”吗? :-)

<span id="<%# tbComments.CliendId %>Label"></span>

#4


Is there something wrong with your JavaScript checkLength() function not appending Label to the Client ID? Then have your Javascript take both?:

您的JavaScript checkLength()函数是否有错误将Label附加到客户端ID?然后让你的Javascript同时使用?:

<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments"
            onChange="javascript:checkLength(<%# tbComments.ClientId %>, <%# tbComments.ClientId %>Label);"
            runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>