views:

2396

answers:

4
<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)?

+3  A: 

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

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

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;
}
Jose Basilio
I would prefer to use e.parentNode.getElementById instead,I tried and it didn't work for me, is there any way?
Shimmy
Because the IDs are dynamically generated, the only way to get a reference to the span is by its position.
Jose Basilio
The client id of the label is the same as the client id of the textbox with a "Label" suffix, so if you know one, you know the other.
Cade Roux
but how can I attach the suffix while I don't have the tbComments's clientId?
Shimmy
Why do you need the actual Id since you can get a reference the elements by traversing the DOM?
Jose Basilio
cuz i don't want it to be depended in the element's index in the DOM.I have to many controls in this ItemTemplate and their amount varies dynmically as well.
Shimmy
You may have to look at a different approach instead of an itemtemplate. I would suggest you post a whole new question explaining your objectives and some more code. I think we will be able to provide you with alternatives.
Jose Basilio
ok, meanwhile im doin it this way, i simply included both in one div, so i don't have this problem.
Shimmy
A: 

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

<span id="<%# tbComments.CliendId %>Label"></span>
belacqua
Sorry, you're right, but not that's the problem.The problem is that it's generated dynamically since it's databound.
Shimmy
A: 

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

<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>
Cade Roux
+1  A: 

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

$(document).ready(function()
{
    $('.myClass').change(function()
    {
         // do your length check...
    });
});
Jake Scott
1) I don't use jQuery in this project2) where do I pass the ClientId?? it seems you didn't get my questionhowever don't bother yourself, It's been answered already.Thanks for your effort.
Shimmy
<asp:TextBox ID="tbComments" cssclass="myClass" runat="server"/>$(document).ready(function(){ $('.myClass').change(function() { vat textbox = $(this); var span = textbox.next(); span.html('comments length: ' + textbox.val().length); });});
Jake Scott
love you man.will surely use it once.
Shimmy