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

Refer

http://forums.asp.net/t/1882931.aspx?Finding+DropDownList+in+DetailsView+s+EditItemTemplate+in+JavaScript


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

I found another way of accomplishing this.

Script

function GetKeyPressInstructions() {
    var count = document.getElementById('<%=txtProgramDescrClientID%>').value;
    //alert(count);
    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;

However;

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