I have an ASP.NET 3.5 GridView containing a list of Documents. When I had all fields within the Gridview including the Select drop-down to select for which States are covered each Document - it worked.

Trying to make it easier to use, I moved the row details to a DetailsView - and then a FormsView - neither types will show the selected previously assigned States for a record.

I have tried the ItemCreated and DataBound events - the EditItemTemplate States control can only be found in the ItemCreated event. I initially had ObjectDataSource DataSourceId binding, but also tried manual binding within the ItemCreated event (to ensure it was there it added states - then iterating through the states and selecting them.

They DO show as selected within the control on exit from ItemCreated event - but when shown on the page - nothing is selected. (If I select new States and Update - it does save the new ones and show them the ItemTemplate dStateList label.)

How can I get the StateList ListBox to retain the selected states and show them in Edit?

protected void DocumentDetail_DataBound(Object sender, EventArgs e)
{
    if (gvDocumentList.SelectedValue == null)
        return;
    int id = (int)gvDocumentList.SelectedValue;
    if (DocumentDetail.CurrentMode == FormViewMode.Edit)
    {
        //HtmlSelect selListStateList = (HtmlSelect)DocumentDetail.FindControl("seselStateList");
        ListBox selListStateList = (ListBox)DocumentDetail.FindControl("lbselStateList");
        if (selListStateList != null)
        {
            // NEVER Hits - selListStateList is always null
            DataSet states = States(); // Get full list of all states
            selListStateList.DataSource = states;
            selListStateList.DataBind();
            // Set selected states from list
            List<string> listOfStates = AdministrationService.GetDMStatesForDoc(id);
            foreach (string state in listOfStates)
            {
                selListStateList.Items.FindByValue(state).Selected = true;
            }
        }
    }
}


protected void DocumentDetail_ItemCreated(Object sender, EventArgs e)
{
    if (gvDocumentList.SelectedValue == null)
        return;
    int id = (int)gvDocumentList.SelectedValue;
    // Get List of states for this document
    List<string> listOfStates = GetStatesForDoc(id);
    if (DocumentDetail.CurrentMode == FormViewMode.ReadOnly)
    {
        Label stateList = DocumentDetail.FindControl("dStateList") as Label;
        if (stateList != null)
        {
            if (listOfStates.Count > 0)
            {
                stateList.CssClass = "";
                stateList.Text = string.Join(", ", listOfStates.ToArray());
            }
            else
            {
                stateList.CssClass = "bg-danger text-white";
                stateList.Text = "None Selected";
            }
        }
    }
    else
    {
        //HtmlSelect selListStateList = (HtmlSelect)DocumentDetail.FindControl("seselStateList");
        ListBox selListStateList = (ListBox)DocumentDetail.FindControl("lbselStateList");
        if (selListStateList != null)
        {
            // THIS DOES Hit
            DataSet states = States();
            selListStateList.DataSource = states;
            selListStateList.DataBind();
            // Set selected states from list
            foreach (string state in listOfStates)
            {
                // THIS Does find and set selected on correct items
                selListStateList.Items.FindByValue(state).Selected = true;
            }
        }
    }
}

<asp:FormView ID="DocumentDetail" AutoGenerateRows="False" EmptyDataText="Select Document to view Details" 
    DataKeyNames="Id" DataSourceID="odsDocument" CssClass="table-sm table-borderless table-striped rounded-top w-100"
    OnItemCreated="DocumentDetail_ItemCreated" BorderStyle="None" BorderWidth="0px" 
    OnItemUpdated="DocumentDetail_ItemUpdated"
    OnItemUpdating="DocumentDetail_ItemUpdating"
    OnItemDeleted="DocumentDetail_ItemDeleted" 
    OnModeChanged="DocumentDetail_ModeChanged"                               
    OnDataBinding="DocumentDetail_DataBound"
    runat="server">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <asp:LinkButton ID="EditButton" Text="Edit" CommandName="Edit" RunAt="server"/>
                        &nbsp;
                        <asp:LinkButton ID="DeleteButton" Text="Delete" CommandName="Delete" RunAt="server"/>
                    </td>
                </tr>
                <tr>
                    <th style="width:50%">Document Name</th>

                </tr>
                <tr>
                    <td><asp:Label ID="lblDocumentName" Width="100%" runat="server" Text='<%# Eval("DocumentName") %>'></asp:Label></td>
                </tr>
                <tr>
                    <th>State List</th>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="dStateList" Style="width: 90%;" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <EditItemTemplate>
            <table>
                <tr>
                    <td>
                        <asp:LinkButton ID="UpdateButton" Text="Update" CommandName="Update" RunAt="server"/>
                        &nbsp;
                        <asp:LinkButton ID="CancelButton" Text="Cancel" CommandName="Cancel" RunAt="server"/>
                    </td>
                </tr>
                <tr>
                    <th>Document Name</th>
                </tr>
                <tr>
                    <td><asp:TextBox ID="etbDocumentName" CssClass="form-control" runat="server" Text='<%# Bind("DocumentName") %>'></asp:TextBox></td>
                    </td>
                </tr>
                <tr>
                    <th>State List</th>
                </tr>
                <tr>
                    <td>                                                    
                        <asp:ListBox ID="lbselStateList" CssClass="form-control multiselect" SelectionMode="Multiple"
                            DataTextField="Long_State" DataValueField="State"
                            runat="server"></asp:ListBox>
                    </td>
                </tr>
            </table>
        </EditItemTemplate>
</asp:FormView>

<script type="text/javascript">
    $(document).ready(function() {                    
        $('.multiselect').multiselect({
            includeSelectAllOption: true,
            maxHeight: 400,
            buttonWidth: '250px',
            nonSelectedText: 'Select state(s)',
            allSelectedText: 'All selected'
        });
    });
</script>
1

There are 1 best solutions below

0
On

I gave up on using the MS controls for document details - really wanted to be able to use them to handle the Save, Update, Delete functionality - safer and simpler in theory... I could not get the embedded list to populate and select the assigned 'multiple' US-states selections to set in the ItemCreated event. Considering the multiple posts on using DetaisView and FormsView - surprised no-one has encountered a problem populating an embedded multiple-select list. Like why does the ItemCreated event set the values for other data-items right, but cannot retain the correct drop-down selections - the other items show correct, just not the drop-down items after the page loads = where are they getting cleared?

Solution was to move them to a div that allowed sharing the div content between 'add new' and 'edit/delete', then just show/hide the correct buttons depending on which state was active. A lot of discrete logic as some values were read-only, present/not present depending on mode and others editable... but worked nicely in the end.