0

I just want to make appear an error message on my multiple DropDownLists if those ones are empty when I click on my Update button.

Problem is : it doesn't work at all. I don't know what I can do actually. I don't even see the InitialValue "Default Value", no error messages, & I'm always redirected to google.com ...

Here is my code :

<div class="col-lg-12">
    <div class="form-group" style="overflow-y: scroll; height: auto; max-height: 545px;">
        <asp:Repeater ID="RepeaterCorrection" runat="server">
            <ItemTemplate>
                <label>
                    <asp:Literal ID="litCorLibelle" runat="server" 
                        Text='<%# Eval("LABEL")%>'>
                    </asp:Literal>
                </label>
    
                <asp:DropDownList ID="ddlCorChamp" runat="server"
                    CssClass="form-control selectpicker" 
                    data-live-search="true">
                </asp:DropDownList>
                <asp:RequiredFieldValidator runat="server"
                    ValidationGroup="validGroup" 
                    forecolor="Red" 
                    Display="Dynamic"
                    ErrorMessage="Error : Insert a Value"
                    ControlToValidate="ddlCorChamp"
                    InitialValue="Default Value" />
            </ItemTemplate>
        </asp:Repeater>        
    </div>

    <asp:Button ID="btnValiderCorrection" runat="server"
        ValidationGroup="validGroup"  
        Text="Update"
        class="btn btn-primary btn-block" 
        CausesValidation="False" 
        UseSubmitBehavior="False" 
        Enabled="True" />

    <span id="lblMessage" class="text-danger" runat="server"></span>
</div>
Private Sub btnValiderCorrection_Click(sender As Object, e As EventArgs) Handles btnValiderCorrection.Click
    Page.Validate()
    If (Page.IsValid) Then
        Response.Redirect("http://www.google.com")
    End If
End Sub

enter image description here

Those DropDownLists got those IDs:

  • "ContentPlaceHolder1_RepeaterCorrection_ddlCorChamp_0"
  • "ContentPlaceHolder1_RepeaterCorrection_ddlCorChamp_1"
  • "ContentPlaceHolder1_RepeaterCorrection_ddlCorChamp_2"

And when I search for "RequiredFieldValidator" on the page, here's what I got : enter image description here

wazz
  • 4,953
  • 5
  • 20
  • 34
Hashka
  • 395
  • 3
  • 10

1 Answers1

0

Your button has

CausesValidation="False"

Change it to true, or remove it (default is true).

You might need to add the ValidationGroup to the dropdown. Not sure.

wazz
  • 4,953
  • 5
  • 20
  • 34
  • First of all, thank you for your time. Then I've tried to remove "CausesValidation="False"" - > still doesn't works. Then to remove "CausesValidation="False"" + add "ValidationGroup" to dropdownlist - > still doesn't works. – Hashka Oct 27 '20 at 13:25
  • I suppose it's possible that the validator *always* sees a value in the dropdown, even when it looks empty. I'd do a thorough search on using the validator with a ddl. [Example](https://stackoverflow.com/questions/2280559/how-to-add-a-requiredfieldvalidator-to-dropdownlist-control). It also seems trickier in a repeater; I've never tried. Look into 'Initial Value' property of validator. – wazz Oct 27 '20 at 13:32