I have a listbox on my asp page, and I was wondering if there was anyway in code behind or javascript to deselect a selected item in my listbox. the selection mode is single.
Any help please? I tried adding a handler on selectedindexchanged...but it doesn't hit it if I am clicking on the same selectedItem.
I fixed my issue as Andrew pointed out, by setting the SelectedIndex = -1;
But, this created weird behavior since now I would have A checkboxList being populated from the selected list item I chose, and there was no Idea to see which list item populated it after wards.
So What I did was added 2 more lines of code on top of what andrew suggested and it worked great.
var index = lstGroups.SelectedIndex;
lstGroups.SelectedIndex = -1;
lstGroups.Items[index].Attributes["style"] = "background-color:lightblue";
Thanks for pushing me in the right direction!
You can use the following workaround if your SelectionMode="Single" and you want to deselect an item on second click.
Create a property in your page to keep the last selected list item value using session. I am assuming the selected value in your listbox is always int
public int ListBoxLastSelected
{
get
{
if (Session["ListBoxLastSelected"] != null)
return Convert.ToInt32(Session["ListBoxLastSelected"]);
return -1;
}
set { Session["ListBoxLastSelected"] = value; }
}
Create the following method
private void EnableListboxDeselect()
{
if (!IsPostBack)
{
// Register a postback event whenever you click on the list item
ClientScriptManager cs = Page.ClientScript;
lstMyList.Attributes.Add("onclick", cs.GetPostBackEventReference(lstMyList, "clientClick"));
Session["ListBoxLastSelected"] = null;
}
if (IsPostBack)
{
// Ensure the postback is from js side
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "clientClick")
{
if (CorpListLastSelected == Convert.ToInt32(lstMyList.SelectedValue))
{
lstMyList.ClearSelection();
CorpListLastSelected = -1;
}
else
{
ListBoxLastSelected= Convert.ToInt32(lstMyList.SelectedValue);
}
}
}
}
On your Page_Load add the below code
protected void Page_Load(object sender, EventArgs e)
{
EnableListboxDeselect();
}
Related
I have a 2 ListViews with same items in both of them. What I want to do is that when a selection is made in one ListView, the same selection should be reflected in the other ListView also. The two ListViews are bound to two different ViewModels but both the ViewModels implement the same interface.
I've overridden the Equals methods in both ViewModels.
The two ListViews are on different XAML pages. The first ListView say LV1 is in Page1.xaml and LV2 is in Page2.xaml. What I want is that when I am changing the selection in LV2 the selection in LV1 should also change( one way only ). I've set x:FieldModifier="public" on LV1 and exposing through a static property of Page1 like this:
public sealed partial class Page1 : Page
{
public static Page1 page1 { get; private set; }
}
And on Page2, I have this :
private async void LV2_ItemClick(object sender, ItemClickEventArgs e)
{
var selected = e.ClickedItem as ISomeCommonInterface;
//Comparision is successful --> Contains() always returns corect value;
if (Page1.page1.LV1.Items.ToList().Contains(selected))
{
Page1.page1.LV1.SelectedItem = null; // this works
Page1.page1.LV1.SelectedItem = selected; // this doesn't work
}
}
I've found that inside the if condition, assignment to null changes the SelectedItem of LV1 to null but the next line doesn't change it to selected ( it remains null ).
add after assignment:
Page1.page1.LV1.Select();
This works for me:
private void LV1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = (sender as ListView).SelectedItem as string;
int index = -1;
for (int i = 0; i < LV2.Items.Count(); i++)
{
if (LV2.Items[i] as string == selected){
index = i;
break;
}
}
// The if becomes obsolete here, it could be replaced by
// if(index >= 0)
if (LV2.Items.ToList().Contains(selected))
{
LV2.SelectedIndex = index;
}
}
There is probably an easier way of getting the index of LV1's SelectedItem in LV2, but it should be enough to get you on the right track.
You can check out the minimal testing app I created that shows that SelectedItem works too.
Method 1 - SelectionMode="Multiple" - both ListViews in sync
You should subscribe the SelectionChanged event on both ListViews - item may not get selected only by click - and there (when selection is changed) you should sync the selection.
private void SyncSelection(object sender, SelectionChangedEventArgs e)
{
ListView listViewToAdd = ReferenceEquals(sender, firstListView) ? secondListView : firstListView;
foreach (var item in e.AddedItems)
{
if (!listViewToAdd.SelectedItems.Contains(item))
{
listViewToAdd.SelectedItems.Add(item);
}
}
foreach (var item in e.RemovedItems)
{
listViewToAdd.SelectedItems.Remove(item);
}
}
Method 2 - SelectionMode="Multiple" - update one after selecting in the other
You should subscribe the SelectionChanged event only on the ListView where items could be selected.
private void SyncSelection(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.AddedItems)
{
secondListView.SelectedItems.Add(item);
}
foreach (var item in e.RemovedItems)
{
secondListView.SelectedItems.Remove(item);
}
}
Method 3 - SelectionMode="Single"
Subscribe the SelectionChanged event on both if you want to make them be in sync or only on the selectable one if you only want to update the second based on the first.
private void SyncSelection(object sender, SelectionChangedEventArgs e)
{
ListView senderListView = (ListView)sender;
ListView listViewToAdd = ReferenceEquals(sender, firstListView) ? secondListView : firstListView;
listViewToAdd.SelectedItem = senderListView.SelectedItem;
}
You may need to replace var with your interface to make it work.
I have 2 comboboxes created with Ajax Toolkit. One of them has a list of systems. I wanted to fill the other combobox with a list of subsystems whenever a system is selected. I did not use DisplayMember or ValueMember and most examples are using them.
.aspx side just in case:
<ajaxToolkit:ComboBox ID="cbox1" runat="server" OnSelectedIndexChanged="cbox1_SelectedIndexChanged" />
Is this doable with what I'm trying? Is the event I used correct for the situation?(I guess not,but other events seem to be unrelated) Let me show you the code:
protected void fillSystemCombo()
{
var sysOperations = new ModelOperations.ConstantSystem.ConstantSystemOperations();
var sys = sysOperations.GetSystemList().TransactionResultList;
foreach (var item in sys)
{
cbox1.Items.Add(item.description);
}
}
This works fine and I can see the systems in my first combobox.
This is what I tried for populating the second one:
protected void cbox1_SelectedIndexChanged(object sender, EventArgs e)
{
var subSysOperations = new ModelOperations.ConstantSubSystem.ConstantSubSystemOperations();
int index = Convert.ToInt32(cbox1.SelectedItem.Value);//i think this should get the id...
var subsys = subSysOperations.GetSubSystemList().TransactionResultList;
foreach (var item in subsys)
{
if (item.sysID == index)
{
cbox2.Items.Add(item.description);
}
}
}
sysID is the foreign key in SubSystem which is the ID of System. By the way, my SelectedIndexChanged event never fired when I was debugging the program even though I clicked on an item in combobox.
I've actually found the answer after carefully reading the parameters taken by Items.Add. It wants a ListItemso if I create a ListItem inside the loop I can finally add my items with both a Text and a Value like this:
foreach (var item in sys)
{
combo1.Items.Add(new ListItem { Text = item.description, Value = item.ID.ToString() });
}
After that I can get the index with
int index = Convert.ToInt32(combo1.SelectedValue);
I have set up the following method when the checkbox list is checked.
protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem list in chk1.Items)
{
if (list.Selected)
{
string name = list.Value.ToString();
}
}
}
I need to display the checked item from the checkbox list. However, for each iteration the selected attribute always comes false. It never satisfies the condition
if (list.Selected)
{
string name = list.Value.ToString();
}
How do I fix this?
Try something like this
var selectedListItems = chk1.Items.Cast<ListItem>().Where(x => x.Selected);
or in your case
var list = chk1.Items.Cast<ListItem>().Where(x => x.Selected);
now you will have a Collection that you can check / code against
also make sure that this code is being fired and or check if there is a PostBack
you can check this by checking if(!Is.PostBack){ }
My money is on you are re-binding the controls on every postback, instead do this:
if (!Page.IsPostBack)
{
// Only bind controls on initial page and let viewstate remember what the user did
}
the ListPicker is a Control from the WP8 Toolkit.
Code:
private void field_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Make sure we don't handle the event during initiation.
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
if (this.field.SelectedItems != null)
{
if (this.field.SelectedIndex != -1)
{
ListPicker_SelectionChanged(sender, e);
//Make needed proffesions visable:
profls.Clear();
foreach (ListPickItem item in field.SelectedItems)
switch (item.Tag)
{
default:
foreach (ListPickItem iitem in profl[9])
profls.Add(iitem);
break;
case 90017:
foreach (ListPickItem iitem in profl[0])
profls.Add(iitem);
break;
case 9000:
foreach (ListPickItem iitem in profl[1])
profls.Add(iitem);
break;
}
}
}
}
}
Please notice that a profession ListPicker's ItemsSource is Data Binded to the profls var.
I modified the Listpicker so that I can also set the SelectedItems property and not only read from it (following this guide) and it works great.
Problem:
The field_SelectionChanged event gets called multiple times whenever I change the field listpicker's selecteditems. (i want it to be called only once..) Another wierd thing is that on one of the last calls the field_SelectedItems is equal to the old selectedItems (the ones before the "change")..
Is it a bug or my problem? (How do I fix it?)
EDIT:
I checked and it appears that it gets called only once if the are no selected items in the listpicker before I select items. (I mean that SelectedItems is empty before I select new items)
Fixed :)
I used the code suggested in this answer: listPicker not updating selection in full mode
if (MyListPicker.SelectedIndex != -1)
{
//Code..
}
I had the same issue of the selectedchange event being called twice.At the end of the listPicker_selectedchange event, set the listpicker selected index to -1.
private void listpicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Code
listpicker.SelectedIndex = -1;
}
Thank you Dan Barzilay!!
I've got a GridView control and Combobox control that are both successfully populated in my Page_Load event (within a block that checks for IsPostBack == false).
I've got an empty button 'btnClick' event handler which will reload the page when clicked. Both the GridView and Combobox controls have their EnableViewState property set to True. The behaviour I was expecting and hoping for was:
Page will reload with the GridView control still populated.
Page will reload with Combobox still populated and the item selected by the user still set as the selected item.
Unfortunately, the behaviour I'm getting is as follows:
GridView control is now empty and shows no data.
Combobox is now empty.
Code as follows:
public MyPage()
{
this.Load += new EventHandler(Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
DataAccessObj daObj = new DataAccessObj();
foreach (DataRow dataRow in daObj.GetAllData())
{
ListItem listItem = new ListItem(dataRow.ToString(), dataRow.Id.ToString());
myCombobox.Items.Add(listItem);
}
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Do nothing
}
What I would like to do is allow the user to select an item from the Combobox. Upon clicking Submit, the GridView would be repopulated (based upon the selected item). The Combobox will remain populated and show the last selected item.
Can someone help explain where I might be going wrong? TIA
When you click your button, the page is posted back, in your page load, if it is a postback you need to databind the grid appropriately you need to add a condition to your page load event like
Firstly on your btn_click, you need to store the id selected with something like:
if (myCombobox.SelectedItem != null)
{
if (int.TryParse(myCombobox.SelectedItem.Value, out reportedById) == false)
{
reportedById = 0;
ViewState["reportedById"] = reportedById; // Need to remember which one was selected
}
}
Then On your Post Back
else (IsPostBack)
{
if (ViewState["reportedById"]) != null)
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(Convert.ToInt32(ViewState["reportedById"]));
IncidentGrid.DataBind();
myCombobox.SelectedItem.Value = ViewState["reportedById"].ToString(); // set combo
}
else
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}