This is a migrated thread and some comments may be shown as answers.

[Solved] Dynamically created RequiredFieldValidator are always validating true

2 Answers 536 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Gary
Top achievements
Rank 2
Gary asked on 20 Jan 2010, 06:30 AM

I'm creating several textboxes and dropdown controls.  Some have a related RequiredFieldValidator control.  The RequiredFieldValidator is created as follows:

        RequiredFieldValidator getValidator(string ctrlNo, bool visible, Label lbl, TextBox txt, RadComboBox rcb)
        {
            string lblName = (lbl==null ? "This " : lbl.Text.Replace(" "," "));
            RequiredFieldValidator rfv = new RequiredFieldValidator();
            rfv.ID = "rfv_" + ctrlNo;
            rfv.Visible = visible;
            if (txt.Visible == true)
                rfv.ControlToValidate = txt.ID;
            if (rcb.Visible == true)
                rfv.ControlToValidate = rcb.ID;
            rfv.ErrorMessage = lblName;
            rfv.ToolTip = lblName;
            rfv.Text = "*";
            rfv.ValidationGroup = "cmsave";
            return rfv;
        }

 This method is called from the following section of code which inserts rows into a table.  Insert location is marked with a PlaceHolder control (ID="tblInsertRow").

                // instantiate each control.
                Label lbl = getLabel(ctrlNo, bShowLbl, property.Label);
                TextBox txt = getTextBox(ctrlNo, bShowTxt, property.ControlWidth);
                RadComboBox rcb = getComboBox(ctrlNo, bShowRcb, property.ControlWidth, list);
                RequiredFieldValidator rfv = getValidator(ctrlNo, bShowRfv, lbl, txt, rcb);

                // STEP 2 - ADD CONTROLS TO A ROW

                if (txt.Visible == true || rcb.Visible == true)
                {
                    // create new table row.
                    TableRow row = new TableRow();

                    // create 1st cell; add label; add cell to row.
                    TableCell cell_0 = new TableCell();
                    if (lbl.Visible == true)
                        cell_0.Controls.Add(lbl);
                    cell_0.ColumnSpan = 1;
                    row.Controls.Add(cell_0);

                    // create 2nd cell; add control; add cell to row.
                    TableCell cell_1 = new TableCell();
                    if (txt.Visible == true)
                        cell_1.Controls.Add(txt);
                    if (rcb.Visible == true)
                        cell_1.Controls.Add(rcb);
                    if (rfv.Visible == true)
                        cell_1.Controls.Add(rfv);
                    cell_1.ColumnSpan = 3;
                    row.Controls.Add(cell_1);

                    // add row at place holder into existing table.
                    tblInsertRow.Controls.Add(row);
                }

 

When the user clicks the "Save" button, I force a validation of all validators as follows:

            // force validation of controls.
            Page.Validate("cmsave");

            if (Page.IsValid == false)
            {
                // display message.
                return;
            }

The problem I'm having is that Page.IsValid is always "true".  I read some older posting that stated that you cannot use RequiredFieldValidator with AJAX, but I don't believe that's true anymore since I've done it before with static controls.  This is the 1st time I'm trying to do this with controls created at runtime.  These controls are created in a User Control that is displayed on a Page with a Master Page.  Could this be a RadScriptManager related issue?  What am I missing?  TIA, Gary.

2 Answers, 1 is accepted

Sort by
0
Gary
Top achievements
Rank 2
answered on 20 Jan 2010, 11:45 PM
I determined that this appears to be a ComboBox control issue.  When I change all ComboBox controls to DropDownList controls, it works.  All the validators evaluate properly.  I will create a sample application to demostrate the issue and open a support ticket ... unless anyone knows why this is happening or what I have overlooked.
0
Accepted
Kalina
Telerik team
answered on 22 Jan 2010, 01:54 PM
Hi Gary,

Thank you for providing a sample project.

Your code looks fine to me - I made just a few changes in it and I think that now validators behave properly.

I changed only the way the first item ( “Select” item ) is added,  I set the InitialValue property of the RequiredFieldValidator to be "Select" , and added the RadComboBox to the validation group.

Now the behavior of the RadComboBoxes is as follows:
-    When nothing is selected – validators fire, and there is no PostBack.
-    When there are items selected in RadComboBoxes – Save button causes PostBack .

The label that "shows" if the page is valid or not is not a good indicator, because it gets its text during the PostBack event of the page.

Kind regards,
Kalina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
ComboBox
Asked by
Gary
Top achievements
Rank 2
Answers by
Gary
Top achievements
Rank 2
Kalina
Telerik team
Share this question
or