How to check a dynamically created control?

I have an asp.net page, some of its controls are created dynamically, these controls are one of the following: text box, calendar or drop-down list.

Do these controls in some cases need to be checked based on a flag read from db?

Is there a way to check dynamically created controls?

+2
source share
4 answers

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;"); 
0
source

Basically, you will need to create your validators using code and attach them to dynamically created controls using code. Then the page will be displayed using your validators on the page.

If verification requires the flag to be read from db, perhaps use a special validator that allows you to configure your specific logic both on the client and on the server for specific verification requirements. You do not need to provide customer verification if you do not want to.

+1
source

You can create validators while creating these controls

0
source

When you arbitrarily create any control, also attach the desired Validator control to it, and you can enable / disable validator validation elements at runtime.

0
source

All Articles