I got a solution for that problem.
One of the main problems that I had with this page that it's ajax-enabled and I need to validate dynamically created controls.
My solution and it works properly, while creating the Control, I added an input attribute to it markes it as it is required or not, and another attribute that marks it as it is a field to be validated or not?
Using Javascript, I go through all input tags with attribute "dynamic control" and based on "to validate attribut", I validate it or not. Simple, right?
Sample Code:
While control creating, mark it like the following
txtBox.Attributes.Add("Type", "T"); // Type of control.
txtBox.Attributes.Add("IsKeyField", "Y"); // Is dynamically created field.
txtBox.Attributes.Add("IsMandatory", "Y"); // Is mandatory or not?
JavaScript code
var inputControls = document.getElementsByTagName("input");
for(var i=0 ; i<inputControls.length ; i++)
{
if ( inputControls[i].getAttribute("IsKeyField") == "Y" )
{
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C"))
{
if(inputControls[i].getAttribute("IsMandatory") == "Y")
{
if(inputControls[i].value == "")
{
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
}
}
}
}
Of course, you can call that code while clicking the required button.
btnUpload.Attributes.Add("onClick", "javascript:if(!ValidateMandatoryFields()) return false;");