Setting Dropdownlist Selecteditem Programmatically

Setting dropdownlist selecteditem programmatically

Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.

list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();

list.SelectedValue = myValue.ToString();

The value of the myValue variable would need to exist in the property specified within the DataValueField in your controls databinding.

UPDATE:
If the value of myValue doesn't exist as a value with the dropdown list options it will default to select the first option in the dropdown list.

How to set a dropdownlist item as selected in ASP.NET?

dropdownlist.ClearSelection(); //making sure the previous selection has been cleared
dropdownlist.Items.FindByValue(value).Selected = true;

How to programmatically set a ASP.NET Web Forms dropdownlist selecteditem and selectedindex

I think what you're probably after here is FindByText, not FindByValue. The value of the item will be the SubjectID column, whereas the text of the item will match the text of the label.

Keeping the selected values in programmatically created DropDownList after button click ASP.NET

I found a solution to the problem after reading this article:

Maintain The State Of Dynamic Added User Control On Each PostBack

I used the ViewState to save ID of the dropdowns to be able to recreate them on Postback. Here is the code:

public Dictionary<Guid, string> DropdownLists
{
get { return ViewState["MyDropdowns"] != null
? (Dictionary<Guid, string>)ViewState["MyDropdowns"] : new Dictionary<Guid, string>(); }
set { ViewState["MyDropdowns"] = value; }
}

public Dictionary<Guid, string> DropdownNames
{
get { return ViewState["DropdownNames"] != null
? (Dictionary<Guid, string>)ViewState["DropdownNames"] : new Dictionary<Guid, string>(); }
set { ViewState["DropdownNames"] = value; }
}

First I added these two class variables which saves their values in the ViewState. Then the when the page loads the first time the method CreateForm() is called which creates all the dropdowns based on the fields in the PDF file. When the page is loaded on postback the dropdowns are created based on the values stored in the ViewState. The code is the following:

protected void Page_Load()
{

//Load the file
doc = new PdfDocument(pdfFileName);

//Get all fields
fields = doc.Fields;

if (!IsPostBack)
{
CreateForm();
}
else
{
RecreateForm();
}
}

private void CreateForm()
{

Dictionary<Guid, string> tempList = new Dictionary<Guid, string>();
Dictionary<Guid, string> tempNames = new Dictionary<Guid, string>();

foreach (PdfField field in fields)
{
//Only display the text fields
if (field.GetType() == typeof(PdfTextField))
{
TableRow tr = new TableRow();
TableCell name = new TableCell
{
Text = field.Name
};
tr.Cells.Add(name);
TableCell options = new TableCell();

DropdownList dropdown = (DropdownList)LoadControl("~/DropdownList.ascx");

Guid nextKey = Guid.NewGuid();
tempList[nextKey] = "dropdown" + nextKey.ToString().Replace('-', '_');
dropdown.ID = tempList[nextKey];

tempNames[nextKey] = field.Name;

options.Controls.Add(dropdown);
tr.Cells.Add(options);

Table1.Rows.Add(tr);
}
}
DropdownLists = tempList;
DropdownNames = tempNames;
}

private void RecreateForm()
{
foreach (var item in DropdownLists)
{
TableRow tr = new TableRow();
TableCell name = new TableCell
{
Text = DropdownNames[item.Key]
};
tr.Cells.Add(name);
TableCell options = new TableCell();

DropdownList dropdown = (DropdownList)LoadControl("~/DropdownList.ascx");

dropdown.ID = DropdownLists[item.Key];

options.Controls.Add(dropdown);
tr.Cells.Add(options);

Table1.Rows.Add(tr);
}
}

I hope this helps anyone out there.

How to SELECT a dropdown list item by value programmatically

If you know that the dropdownlist contains the value you're looking to select, use:

ddl.SelectedValue = "2";

If you're not sure if the value exists, use (or you'll get a null reference exception):

ListItem selectedListItem = ddl.Items.FindByValue("2");

if (selectedListItem != null)
{
selectedListItem.Selected = true;
}

How can you set the selected item in an ASP.NET dropdown via the display text?

You can try:

ddItems.Items.FindByText("Hello, World!").Selected = true;

Or:

ddItems.SelectedValue = ddItems.Items.FindByText("Hello, World!").Value;

Note that, if you are not certain that an items exists matching your display text, you may need to check the results of FindByText() for null.

Note that I use the first approach on a multiple-select list, such as a CheckBoxList to add an additional selection. I use the second approach to override all selections.

Change databound Drop Down List programmatically

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


Related Topics



Leave a reply



Submit