locked
How to check for null values in a ListView's update command? RRS feed

  • Question

  • User260076833 posted

    Hello,

    I have a ListView where the user may edit entries using the EditItemTemplate:

                <asp:ListView ID="lvw_Zuordnung" runat="server" DataSourceID="src_Zuordnung" DataKeyNames="idx" ...>
    
                    <LayoutTemplate>
    ... </LayoutTemplate> <GroupTemplate>
    ... </GroupTemplate> <ItemTemplate>
    ... </ItemTemplate> <InsertItemTemplate>
    ... </InsertItemTemplate> <EditItemTemplate> <td align="left"></td> <td> <asp:HiddenField ID="fld_idx" runat="server" Value='<%# Bind("idx")%>' /> <asp:DropDownList ID="lst_Anlage" runat="server" DataSourceID="src_Anlage" AutoPostBack="True" DataValueField="idx" DataTextField="sym" OnSelectedIndexChanged="lst_Anlage_SelectedIndexChanged"> </asp:DropDownList> </td> <td> <asp:DropDownList ID="lst_Schluessel" runat="server" DataSourceID="src_Schluessel"
    DataValueField="idx" DataTextField="sym" SelectedValue='<%# Bind("schluessel")%>' OnDataBinding="lst_Schluessel_DataBinding"> </asp:DropDownList> </td> <td> <asp:DropDownList ID="lst_Modus" runat="server" AutoPostBack="True" SelectedValue='<%# Bind("modus")%>'> <asp:ListItem Value="F" Text="fest" /> <asp:ListItem Value="T" Text="temporär" /> </asp:DropDownList> </td> <td></td> <td> <asp:LinkButton ID="btn_Update" runat="server" CausesValidation="False" CommandName="Update" Text="Aktualisieren"></asp:LinkButton> <asp:LinkButton ID="btn_Cancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Abbrechen"></asp:LinkButton> </td> </EditItemTemplate> </asp:ListView>

    The DropDownList "lst_Schluessel" may contain an empty entry (""), which means that nothing is selected at the moment.

    When the user then clicks on "Update", the SQL update statement causes an error.

    How can I make sure that the update command is not executed when the bound field "schluessel" is empty?

    Thanks
    Magnus

    Monday, October 16, 2017 1:16 PM

All replies

  • User475983607 posted

    How can I make sure that the update command is not executed when the bound field "schluessel" is empty?

    Add a validation server control or check if the field is empty in the code behind.

    Monday, October 16, 2017 1:43 PM
  • User-335504541 posted

    Hi Yeoman,

    I think you could also try to use jquery to check it.

    Use onclientclick event on the update button.

    For example:

    <asp:LinkButton ID="btn_Update" runat="server" CausesValidation="False" CommandName="Update" Text="Aktualisieren" OnClientClick="return checkval();"></asp:LinkButton>
    
    <script> function checkval() { var i = 0; $("select").each(function () { var selectedval = $(this).find(":selected").text(); if (selectedval == "") { i++; } }); if (i == 0) { return true; } else { alert("Some Value is null"); return false; } } </script>

    Best Regards,

    Billy

    Wednesday, October 18, 2017 1:54 AM