Home > Software design >  ASP.NET Drop down list required field validator not working
ASP.NET Drop down list required field validator not working

Time:08-30

When adding a required field validator to a drop down list, it will not make it required.

Here is the code:

<td style="height: 14px">Parent</td>
<td style="height: 14px">
    <asp:DropDownList ID="ddlParentReportingCat" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlParentReportingCat_SelectedIndexChanged" CausesValidation="True"></asp:DropDownList>
    <asp:RequiredFieldValidator ID="rfvParentReportingCat" runat="server" InitialValue="0" ControlToValidate="ddlParentReportingCat" ErrorMessage="*" Display="Dynamic" ></asp:RequiredFieldValidator>
</td>

This is the code behind:

protected void ddlParentReportingCat_SelectedIndexChanged(object sender, EventArgs e)
{
    var parentReportingCategoryId = ddlParentReportingCat.SelectedValue.ParseInt32();
    
    if (parentReportingCategoryId.IsNotNullOrZero())
    {
        ddlChildReportingCat.DataSource = reportingCategoryAdministrator.GetByParent(parentReportingCategoryId);
        ddlChildReportingCat.DataTextField = "ReportingCategoryName";
        ddlChildReportingCat.DataValueField = "Id";
        ddlChildReportingCat.DataBind();
    }

    if (parentReportingCategoryId.IsNullOrZero())
    {
        return;
    }

    PageFunction.SetDdlValue(ddlChildReportingCat, ProductClass.ReportingCategoryId.ToString());
    SetEnabledStateOfDdlTaxCode();
    SetProductFormData();
    SetMiniWizardLinksData();
}

public static void BindParentReportingCatDDL(DropDownList ddl)
{
    BindDDL(ddl, "ReportingCategory", "ReportingCategoryID",
            "SELECT ReportingCategoryID, ReportingCategory FROM dbo.tblReportingCategory WHERE (ParentReportingCategoryID IS NULL) ORDER BY ReportingCategory");
}

The data in the list is from the database.

CodePudding user response:

Set InitialValue="-1" and got it working! Thanks!

  • Related