tags:

views:

937

answers:

4

Hi all,

I have an asp.net page, some of its controls are created dynamically, these controls are one of the following; textbox, calendar, or dropdownlist.

These controls in some cases, should be validated based on flag read from db?

Is there any way to validate dynamically created controls?

A: 

You can create validators at the same time you create those controls

Sergio
I did this, but not working!
Ahmed
Post some code? That way we can perhaps help with what's wrong?
Ian Devlin
A: 

When you are creating any control dynamically, also attach with them desired Validator control and you can enable/disable validator controls at run time.

Sachin Gaur
+1  A: 

Basically you will need to create your validators via code and attach them to the dynamically created controls via code too. The page will then render with your validators in the page for you.

If validation requires a flag to be read from the db then perhaps use a custom validator which will allow you to set up your specific logic on both the client and server for your specific validation requirements. You don't have to provide client validation if you don't want to.

Peanut
A: 

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;");
Ahmed