我有一个包含文档列表的ASP.NET 3.5 GridView。 当我在Gridview中拥有所有字段时,包括“选择”下拉列表以选择每个文档涵盖哪些州-它起作用了。
为了简化操作,我将行详细信息移至DetailsView-然后移至FormsView-这两种类型都不会显示记录中先前选定的状态。
我尝试了ItemCreated和DataBound事件-EditItemTemplate States控件只能在ItemCreated事件中找到。最初,我具有ObjectDataSource DataSourceId绑定,但还尝试在ItemCreated事件中进行手动绑定(以确保它添加了状态-然后遍历状态并选择它们。
它们确实显示为在ItemCreated事件退出控件中处于选中状态-但是在页面上显示时-未选择任何内容。 (如果我选择新状态并更新-它会保存新状态并显示给他们ItemItem dStateList标签。)
如何获取StateList列表框以保留所选状态并将其显示在“编辑”中?
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>
答案 0 :(得分:0)
我放弃了使用MS控件获取文档详细信息-确实希望能够使用它们来处理“保存,更新,删除”功能-理论上更安全,更简单...我无法获取嵌入列表并选择要在ItemCreated事件中设置的分配的“多个”美国州选择。考虑到有关使用DetaisView和FormsView的多个帖子,令人惊讶的是,没有人遇到填充嵌入式多选列表的问题。就像为什么ItemCreated事件会为其他数据项设置正确的值,却不能保留正确的下拉选择项-其他项显示正确,只是页面加载后的下拉项不是正确的=将从何处清除它们?
解决方案是将它们移动到允许在“添加新”和“编辑/删除”之间共享div内容的div,然后仅根据活动状态显示/隐藏正确的按钮。许多离散逻辑,因为某些值是只读的,取决于模式而存在/不存在,而其他值可编辑...但是最终效果很好。