0

Currently, I'm trying to insert a new item into a drop-down list. It seems like nothing is working. Nothing is added to the dropdown list. It finds the control find I belive but does not insert a new item. Is my syntax off or something?

protected void livFact_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        ListViewItem item = livFact.Items[e.NewEditIndex];
        DropDownList ddl = (DropDownList)item.FindControl("ddlLocation");

        ddl.Items.Insert(0, new ListItem("--Location--", "0"));

    }

 <%--Edit Item Template--%>
    <EditItemTemplate>
        <tr>
            <td>
                <asp:Button runat="server" ID="btnModifySave" SkinID="btnListView" class="btn btn-success btn-sm" CausesValidation="true" CommandName="Update" Text="Save" ValidationGroup="ModifySave" />
                <asp:Button runat="server" ID="btnModifyCancel" SkinID="btnListView" class="btn btn-danger btn-sm" CausesValidation="false" CommandName="Cancel" Text="Cancel" />
            </td>
            <%--Copy Area Start Set Enabled="true" --%>
            <td>
                <asp:DropDownList runat="server" ID="ddlLocation" class="form-control" Enabled="true" DataSourceID="sdsDropDownListLocation" DataTextField="Location" DataValueField="LocationID" AppendDataBoundItems="true" />
                <asp:Label runat="server" ID="lblLocationID" Visible="false" Text='<%# Bind("LocationID") %>' />
                <br />
                <div class="alert alert-warning rounded">
                    <asp:Label runat="server" Text="Current Location editing: " />
                    <asp:Label runat="server" CssClass="text-primary font-weight-bold" ID="lblLocation" Text='<%# Bind("Location") %>' />
                </div>
            </td>
            <td class="text-center">
                <asp:TextBox runat="server" ID="txtFact" class="form-control" Enabled="true" Text='<%# Bind("Fact") %>' TextMode="MultiLine" Rows="5" />
                <asp:RequiredFieldValidator runat="server" ControlToValidate="txtFact" Enabled="true" Display="Dynamic" ErrorMessage="A Fact is required." Text="*" ForeColor="Red" SetFocusOnError="true" ValidationGroup="ModifySave" />
            </td>
            <%-- Copy Area End--%>
        </tr>
    </EditItemTemplate>
Isiah Jones
  • 94
  • 1
  • 12
  • You’ve posted WebForms code, not ASP.NET Core code. Please update your question’s tags. – Dai Apr 25 '20 at 23:38
  • More about the context is needed. What is livFact? (ListView presumably), is this inside an update panel? Can you please add both your aspx and code behind source to your question? – Oguz Ozgul Apr 25 '20 at 23:58
  • @OguzOzgul I cannot post all of that code. I can post bits and pieces thou. So, I'm finding a drop-down list control inside the listview livFact which is in the EditItemTemplate. – Isiah Jones Apr 26 '20 at 00:04

1 Answers1

0

I found a simpler approach to solve my problem. Instead of using the code behind and c#, you can solve the problem on the aspx page itself. You can simply add a ListItem and put anything you want into the drop-down list on top of any data your binding to the drop-down list from a database. I would then suggest creating a Required Field Validator with an Initial Value to stop it from saving to the database if the user clicks save with the default option selected.

<asp:DropDownList runat="server" ID="ddlLocation" class="form-control" Enabled="true" DataSourceID="sdsDropDownListLocation" DataTextField="Location" DataValueField="LocationID" AppendDataBoundItems="true">
     <asp:ListItem Text="--Location--" Value="-1" Selected="True" />
</asp:DropDownList>

Found my answers here.

Isiah Jones
  • 94
  • 1
  • 12