I'm selecting a ListItem as I add it to a ListItemCollection. Then I use that ListItemCollection as a datasource for a DropDownlist but the Selected List Item is not being selected after databind. Here is an example of the code:
ListItemCollection items = new ListItemCollection();
ListItem item;
item = new ListItem("Option 1", "1");
items.Add(item);
item = new ListItem("Option 2", "2");
item.Selected = true;
items.Add(item);
ddl1.DataSource = items;
ddl1.DataBind();
I'm trying to get this to work so I can return only a list of items, instead of a list of items and the selected value. Is there a way to make the DropDownList select the selected ListItem from the ListItemCollection (or any other type of collection)?
Hmm... this seems like a strange method to take for accomplishing this, you should be able to do something along these lines:
ddl1.Items.Clear();
foreach(ListItem item in items)
{
ddl1.Items.Add(item);
}
Which should solve your selection issue...
I don't think you can set the selected value before you bind to the drop down. I think you have to do it after it has been bound.
Just set the SelectedValue property of your DropDownList :
ddl1.SelectedValue = "Option 2";
Here I'm using a literal string, but it's best to the item.Text value to set it. You can use it before or after the DataBind(), it works either way.
Related
I'm adding items to a ListView and assigning tags to them like this:
ListView1.Items.Add("FirstItem").Tag = "yellow"
Then, when I click on a button, I have all the items with the "yellow" tag disappear from the list:
For Each listItem As ListViewItem In ListView1.Items
If listItem.Tag = "yellow" Then
listItem.Remove()
End If
Next
My problem is that I want to assign multiple tags to a single item, so that when I click the button, items with two or more tags will be removed instead. How could I do this?
You're not "assigning tags". There's no such thing as a tag. What you're doing is assigning Strings to the Tag properties. That Tag property is type Object specifically so that you can assign whatever object you want to it, so you need to assign an object that can contain the information you want. There are multiple ways to do that. You could use a delimited String, e.g.
ListView1.Items.Add("FirstItem").Tag = "yellow|blue|red"
and:
If CStr(listItem.Tag).Contains("yellow") Then
You could also use a String array or collection, e.g.
ListView1.Items.Add("FirstItem").Tag = {"yellow", "blue", "red"}
and:
If DirectCast(listItem.Tag, String()).Contains("yellow") Then
One advantage of the second option is that it makes using partially equal values easier. For instance, if you did this:
ListView1.Items.Add("FirstItem").Tag = "yellow|dark blue|red"
and:
If CStr(listItem.Tag).Contains("blue") Then
then your item would match when it logically shouldn't. If you do this:
ListView1.Items.Add("FirstItem").Tag = {"yellow", "dark blue", "red"}
and:
If DirectCast(listItem.Tag, String()).Contains("blue") Then
then your object will not match.
When use a node to show combobox's items only display the fisrt item I select.
val sspSelected = SimpleStringProperty()
val myItems = FXCollections.observableArrayList("Item 1", "Item 2","Item 3")
combobox<String>(sspSelected){
items = myItems
cellFormat {
graphic = cache{
label(it)
}
}
}
label(sspSelected)
No selected item
First item selected
Third item selected
You are using cache without supplying a cache key, so the graphic node for the cell is calculated from the first value it sees. Simply supply a unique id, in this case the string value as a cache key:
graphic = cache(it) {
label(it)
}
In one of our ASP.NET MVC 4 views, we have a DropDownListFor (along with other html controls) as follows:
#Html.DropDownListFor(model=>model.ProductType,
(List<SelectListItem>)ViewBag.ProductType,
"--- Select Product Type ----")
The list is getting populated from a database lookup table. When we click on the submit button, all the controls values including the selected value of the dropdownlist get inserted into the database successfully. But when the page is displayed the dropdown's selected value always shows "--- Select Product Type ----" instead of the value that was inserted into the database.
Please help. Thanks.
You need to set the selected value in your SelectList inside your Controller i.e. ViewBag.ProductType.
Have a look at the overloads for SelectList class.
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.118).aspx
public ActionResult MyMethod()
{
var items = db.ProductType.ToList();
var selectedItem = items.FirstOrDefault(x => get desired selected item);
ViewBag.ProductType =
new SelectList(items, "ProductID", "ProductType", selectedItem);
}
Is there any way to move through datarepeater's items through code, as we run loop and move through the items in a list / combo box?
Thanks
Furqan
The code from Schmelter changes the current row, but this might produce undesired effects since it can update the UI and causes other data-handling events to fire. It's not necessary to change the CurrentItemIndex to loop through the DataRepeaterItems. Each DataRepeaterItem is just a Control object in the DataRepeater.Controls collection. Here is an alternative (in C#):
using Microsoft.VisualBasic.PowerPacks;
foreach ( DataRepeaterItem rowItem in dataRepeater1.Controls )
{
int itemIndex = rowItem.ItemIndex;
// If it's bound, get the underlying data object
object dataItem = BindingSource1.List[itemIndex];
// Add code for each rowItem of the dataItem
// All controls on the DataRepeateItem can be obtained from rowItem.Controls
}
This should work:
For i As Integer = 0 To Me.DataRepeater1.ItemCount -1
Me.DataRepeater1.CurrentItemIndex = i
Dim item As DataRepeaterItem = Me.DataRepeater1.CurrentItem
Next
I have a problem with AutoCompleteBox. I wanted to use it as editable combobox. So I created custom control inheriting from AutoCompletBox and added two dependency properties named as SelectedValue (for binding to DataContext) and SelectedValuePath. When user selects an item, my custom control updates SelectedValue as the following way:
string propertyPath = this.SelectedValuePath;
PropertyInfo propertyInfo = this.SelectedItem.GetType().GetProperty(propertyPath);
object propertyValue = propertyInfo.GetValue(this.SelectedItem, null);
this.SelectedValue = propertyValue;
It works.
Reversely, when underlying datacontext changed, SelectedValue is also changed; so custom control's SelectedItem also has to change:
if (this.SelectedValue == null)
{
this.SelectedItem = null; //Here's the problem!!!
}
else
{
object selectedValue = this.SelectedValue;
string propertyPath = this.SelectedValuePath;
if (selectedValue != null && !(string.IsNullOrEmpty(propertyPath)))
{
foreach (object item in this.ItemsSource)
{
PropertyInfo propertyInfo = item.GetType().GetProperty(propertyPath);
if (propertyInfo.GetValue(item, null).Equals(selectedValue))
this.SelectedItem = item;
}
}
}
What troubles me is when SelectedValue is null. Even if SelectedItem is set to null, Text property is not cleared, if it was manually edited by user. So SelectedItem = null but AutoCompleteBox displays a text entered manually. Can someone show me right way of resetting AutoCompleteBox.SelectedItem property?
What a coincidence... I was just doing the same thing today. In fact don't even bother to set SelectedItem = null. You can just set Text = String.Empty and both the text area and the SelectedItem will be cleared.
That doesn't work in an MVVM set up
It is solve the problem:
selectedChange += (e,v) => {if (selected item == null) Text = String.Empty};
but it couse to other problem - when you select item, and insert later...