我有一个包含文档列表的 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"/>
<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"/>
<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>