ASP.NET List Item IDs - asp.net

The <li> tags generated by ASP.NET when I programatically add ListItems to my ListBox control do not have ID attributes. Is there any way that I can get them to have IDs so that I can get references to the elements from Javascript?

Use the ListItem's attribute collection, like this...
ListItem item = new ListItem();
item.Attributes.Add("id", "myId");
myList.Items.Add(item);

Related

Fill only selected checkbox items from checkboxlist into new list

How can i set all selected items from an Checkboxlist into a list?
I tried this code but when i run it all checkbox items where added? I only want the selected items
List<string> WeeklyDays = (from l in CheckBoxListWeeklyDays.Items.Cast<ListItem>() select l.Value).ToList();
Your snippet is C#/ASP.NET not vbscript/classic-asp
In your query add a WHERE statement to look at the "Selected" attribute of each ListItem, similar to:
List WeeklyDays = (from l in CheckBoxListWeeklyDays.Items.Cast() where l.Selected select l.Value).ToList();
Taken from: How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?

ListItem additional custom values

I am using dropdownlist in asp.net, it has collection of ListItem that represents the items of the dropdownlist, each ListItem has only two fields to hold the data, Value and Text fields, but those are not enough I would like to hold more data for each item. Let's say Text1 and Text2 in additional fields, but at the same time I would like to preserve the same behavior of the DataBind() method of the dropdownlist.
Sure, you can use the Attributes collection:
ListItem li = new ListItem("myText", "myID");
li.Attributes.Add("data-x", "xx");
dropdown.Items.Add(li);
This will give you this HTML:
<select name="dropdown" id="dropdown">
<option value="myID" data-x="xx">myText</option>
</select>
I suggest you prefix your custom attributes with "data-": http://html5doctor.com/html5-custom-data-attributes/
If you want to be able to preserve the items, try setting the AppendDataBoundItems property to true. Then you can programmatically add more items:
dropDown.Items.Add("Another Item", "2");
and preserve the original ones.

Weird behavior while creation of two Telerik RadComboBox items programmatically

I've two Telerik RadComboBox controls, while adding of their items using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new RadComboBoxItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlTelerik1.Items.Add(item);
ddlTelerik2.Items.Add(item);
}
at runtime, the first combobox has zero items i.e. items are not added to it while items are added to the second one!
I tried the same for ASP.NET DropDownList using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new ListItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlAspNet1.Items.Add(item);
ddlAspNet2.Items.Add(item);
}
it is working properly and items got added to both of them.
Any ideas what could be the reason behind this weird behavior?
The difference is that the RadComboBox item is a control like the RadComboBox or DropDownList controls, so it's a class that indirectly inherits from WebControl, and can only have one instance on the page. It's not like a ListItem (which inherits from Object).

Telerik RadComboBox AutomaticLoadOnDemand

I use Telerik RadComboBox in my project. I set EnableAutomaticLoadOnDemand="true" on RadComboBox. It works good but when I want to set selected item on load of page event, it doesn't show selected item
With load on demand mode, so combobox does not have any item. It just has item(s) when you action on it.
In my opinion, you should get the specific item and add it to combobox manually on page load event like this way (I'm not sure the structure, just an idea.)
if (!IsPostBack)
{
var itemSelected = service.GetById(Id); //item Id
this.combobox.Items.Add(new RadComboboxItem(itemSelected.Id, itemSelected.Name));
this.combobox.SeletedValue = Id.ToString();
}
RadCombo allows to set up SelectedValue and Text properties even through there are no items in it.

Change databound Drop Down List programmatically

I have a drop down list that is populated in the page load event from a database table.
The drop down has a DataTextField set to a project name and the DataValueField set to the project id (interger).
Later I change the dropdowlist selected item with this code in the selectedindexchanged event of a gridview
GridViewRow row = GridView1.SelectedRow;
ddlProjectList.SelectedItem.Text = row.Cells[2].Text;
Does Changing the drop down list with this code cause the DataValueField property to change to the correct Project ID number also? If not is there a better way to do this?
=============================================EDIT
actually this code seems to be adding an additional item to the list so that the project i set with this code is listed twice so I don't think my code is correct
This does not change anything else than the text of the selected item in your DropDownList. Do you want to change the text and value of the project, or do you want to select the project from the DropDownList that relates to the selected row in the grid?
To change the project name and id, you'll have to change it in the data source behind the ProjectList. But if you just want to select the related project, you can use this:
var row = GridView1.SelectedRow;
if (ProjectList.Items.FindByText(row.Cells[2].Text) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text).Selected = true;
}
Setting SelectedItem.Text does not actually change the selected item by its text, it changes the text of the currently selected item. You need to use ddl.Items.FindItemByText (or it may be worded FindByText, I forget at the moment) to find the item, then set the ListItem.Selected property to true.
HTH.
You can do
ddlProjectList.FindByText(row.Cells[2].Text).Selected = true;
This will actually set it.

Resources