ListItem additional custom values - asp.net

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.

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#?

Place item at specific position in dropdown list

Can we place/keep dropdownlist item at particular position. Say for example I want to bind dropdownlist to datasource and then add new item which is placed at particular position in dropdownlist. How can this be possible.
You can use Insert method.
dropDownList.Items.Insert(index, new ListItem("Default Panel", "0"));

how to set the value of a dropdown to its default on pageload

Page has Repeater Control displays students records, when click on any record it displays total info.
it has dropdown list displays list of all departments [ not static items ] comes from
db.<asp:DropDownList ID="ddl_name" runat="server" DataSourceID="Employee" DataMember="Technical"
DataTextField="Last_name"
DataValueField="Emp_code"
FirstItemText="Select Item"
FirstItemValue="0"
BoundColumnName="LastName"
BoundDataMember="DeptCode"
Clear="true"
/> Thanks...
by default it should display the first item..and when the user selects a department and save it, the page reloads and again it should show the default value.
You can do it via DropDownList.SelectedIndex property:
ddl.SelectedIndex = 0;
But I would recommend using Post-Redirect-Get instead and after a redirect-get you will have your dropdown set to default.
You may also try storing selected value in a variable or Session[""]

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.

ASP.NET List Item IDs

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);

Resources