Hello there...

can somebody guide me on this…

I have a panel (say, panel1) inside another panel (say, outer-panel)….The panel1 is visible only when a checkbox is checked in the outer-panel.

I have many text boxes in the panel1. I have to put a validation on all the boxes.
I have to enable the validations on certain conditions,say, if textbox 1 is filled only then the required-field validator for textbox 2 should be enabled and vice-versa.
I did write code in the page_load for this.

if (txt1.Text != "")
            {
                required_txt2.Enabled = true;

            }
            else
            {
                required_txt2.Enabled = false;
            }
            if (txt2.Text != "")
            {
                required_txt1.Enabled = true;
            }
            else
            {
                required_txt1.Enabled = false;
            }

This code does enable the validator but does not show any error if one the textboxes is not filled and the page gets submitted normally.
the asp code for the validators is,

<asp:RequiredFieldValidator ID="required_txt1" runat="server" ErrorMessage="Please enter the text."
                    ControlToValidate="txt1" EnableClientScript="false" Enabled="false" Display="Dynamic"
                    SetFocusOnError="true" ValidationGroup="Submit">*</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="required_txt2" runat="server" Enabled="false" EnableClientScript="false"
                    ErrorMessage="Please enter the text 2." ControlToValidate="txt2" SetFocusOnError="true"
                    Display="Dynamic" ValidationGroup="Submit">*</asp:RequiredFieldValidator>

Can somebody please tell me what could be the reason…thanks in advance….

If u invisible the panel1 when user click on checkbox, u cant see the inside panels beacuse inside panels is added into panel1.

I may be overlooking the obvious here since i dont really work in ASP.NET much, but doesnt Enabled = "false" in your validators declaration mean that it is not active?

Your problem lies in the page life cycle for .NET. The process of the page load causes validation to occur, then event handlers. This causes all validate controls to be processed, then the fields are set to invalid and setting up the validate functions to be called in the next step.

The way I tend to validate fields like that is to create a separate validate function that I call from the process button. The function returns true if the form is valid to my specifications and the form is processed as it should be. If any of the controls do not validate how I want them to, I add a label control to a panel dynamically when the control is found to be invalid and set the bool value to false, which I return after all the other controls have been checked.

That process may not be the best way to do it (I'm not experienced enough to know), but it works well for me when I have dependent text/check boxes/radio buttons as well as required field/regex combos on text boxes.

thank you for the responses....

The way I tend to validate fields like that is to create a separate validate function that I call from the process button. The function returns true if the form is valid to my specifications and the form is processed as it should be. If any of the controls do not validate how I want them to, I add a label control to a panel dynamically when the control is found to be invalid and set the bool value to false, which I return after all the other controls have been checked.

Can you please give me an example code to better understand this..thank you

Sure.

ASP code:

<asp:Panel id="pnlValidate">
<asp:Textbox id="txtTest">
<asp:Button Text="Save" id="btnSave" onClick="btnSave_Click">

C# code:

protected void btnSave_Click(object sender, EventArgs e)
{
    // Only process the page if the form validates
    if(ValidateForm())
        { processPage... }
}

protected bool ValidateForm()
{
    // Set the return value to true to start
    bool isValid = true;

    if(txtTest.Text == null || txtTest.Text == string.empty() || txtTest.Text == "")
    {
        // Return value should now be false because the field was required, but empty
        isValid = false;

        // Create a new label
        Label errorLabel = new Label();
        errorLabel.Text = "Test is a required field!"

        // Add the label to the panel's controls
        pnlValidate.Controls.Add(errorLabel);
    }

    return isValid;
}

Obviously this example doesn't take care of your exact situation, but it's quite easy to adapt it to checking multiple fields in an if statement. It also may help that if you use this method, to create a method that takes a string (or more) and returns a label with that string as its text. Cuts down on the code when you start adding attributes to the label.

thank you all for the responses..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.