I have a solution to this problem. One of the main problems I encountered with this page is ajax support, and I need to check dynamically generated controls.
My solution, and it works correctly, creating a control, I added an input attribute that it marks as required or not, and another attribute that marks it as this field that should be checked or not?
Using Javascript, I look at all input tags with the attribute "dynamic control" and based on "to check the attribute", I check it or not. Just right?
Sample code: When creating a control, mark it as follows
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 this code by clicking the desired button.
btnUpload.Attributes.Add("onClick", "javascript:if(!ValidateMandatoryFields()) return false;");
Ahmed source share