0

我有一个包含文档列表的 ASP.NET 3.5 GridView。当我在 Gridview 中拥有所有字段时,包括选择下拉菜单以选择每个文档涵盖的状态 - 它起作用了。

为了使其更易于使用,我将行详细信息移动到 DetailsView - 然后是 FormsView - 这两种类型都不会显示为记录选择的先前分配的状态。

我已经尝试过 ItemCreated 和 DataBound 事件 - EditItemTemplate States 控件只能在 ItemCreated 事件中找到。我最初有 ObjectDataSource DataSourceId 绑定,但也尝试在 ItemCreated 事件中手动绑定(以确保它在那里添加了状态 - 然后遍历状态并选择它们。

它们确实在退出 ItemCreated 事件时在控件中显示为已选中 - 但是当显示在页面上时 - 没有选择任何内容。 (如果我选择新的状态和更新 - 它会保存新状态并向它们显示 ItemTemplate dStateList 标签。)

如何让 StateList ListBox 保留所选状态并在编辑中显示它们?

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>
4

1 回答 1

0

我放弃了使用 MS 控件来获取文档详细信息 - 真的希望能够使用它们来处理保存、更新、删除功能 - 理论上更安全、更简单......我无法让嵌入列表填充并选择分配“多个”美国州选择以在 ItemCreated 事件中设置。考虑到有关使用 DetaisView 和 FormsView 的多个帖子 - 令人惊讶的是,没有人遇到填充嵌入式多选列表的问题。就像为什么 ItemCreated 事件正确设置其他数据项的值,但不能保留正确的下拉选择 - 其他项目显示正确,而不是页面加载后的下拉项目 = 它们在哪里被清除?

解决方案是将它们移动到允许在“添加新”和“编辑/删除”之间共享 div 内容的 div,然后根据处于活动状态的状态显示/隐藏正确的按钮。很多离散逻辑,因为一些值是只读的,存在/不存在取决于模式和其他可编辑的......但最终工作得很好。

于 2018-11-26T16:48:54.103 回答