Validate TextBox inside EditItemTemplate of DetailsView using JavaScript in ASP.Net

Last Reply on Apr 07, 2015 02:55 PM By noobie

Posted on Apr 07, 2015 06:42 AM

How can we pass or find an ID when using getElementById in a detailsView and Edit-ItemTemplate


I am trying to find the elements

ie textboxes and labels. I am trying to do a character count on the textboxes using getElementById..Is there another way

Posted on Apr 07, 2015 09:44 AM


Posted on Apr 07, 2015 02:55 PM Modified on on Apr 08, 2015 01:49 AM

I found another way of accomplishing this.


function GetKeyPressInstructions() {
    var count = document.getElementById('<%=txtProgramDescrClientID%>').value;
    var count1 = document.getElementById('<%=lblDescriptionCountClientID%>');
    if (count.value.length > 0) {
        count1.innerHTML = (count.value.length);
    } else {
        count1.innerHTML = '';

After creating properties in the code behind and setting the values to the strings/variables. 

I then use a string builder to accomplish my need. ie, to get the count and display it on a label

if I tried using :

description.Attributes.Add("OnKeyUp", "GetKeyPressInstructions");

To get the method from the js it wont find it, because of the editItemTemplate.

txtProgramDescrClientID = dgvProgram.FindControl("txtProgramDescr").ClientID;
lblDescriptionCountClientID = dgvProgram.FindControl("lblDescriptionCount").ClientID;


StringBuilder str = new StringBuilder();
str.Append("var count = document.getElementById('");
str.Append("var count1 = document.getElementById('");
str.Append("if (count.length > 0) { count1.innerHTML = (count.length); } else { count1.innerHTML = ''; }");
description.Attributes.Add("OnKeyUp", str.ToString());