DetailsView/FormView - multiselect (ListBox or Select) in EditItemTemplate loses selections after Edit ItemCreated
up vote
0
down vote
favorite
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"/>
<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>
listbox detailsview bootstrap-multiselect edititemtemplate
add a comment |
up vote
0
down vote
favorite
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"/>
<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>
listbox detailsview bootstrap-multiselect edititemtemplate
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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"/>
<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>
listbox detailsview bootstrap-multiselect edititemtemplate
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"/>
<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>
listbox detailsview bootstrap-multiselect edititemtemplate
listbox detailsview bootstrap-multiselect edititemtemplate
edited Nov 9 at 18:56
asked Nov 9 at 18:50
ToddN
164
164
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231710%2fdetailsview-formview-multiselect-listbox-or-select-in-edititemtemplate-loses%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown