Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select with DropDownList.text

Tags:

c#

asp.net

c#-4.0

I am working on an Asp.NET project and I am trying to set the selected value of a dropdown list with a text property. For example i have i.e an item in the dropdown list with text test. Programmatically can i set it to selecteditem by Text?. I am using the follwing code but is not working.

protected void Page_Load(object sender, EventArgs e)
{
    string t = "test";
    drpFunction.Text = t; 
}

But is not working. Any suggestions ?

like image 486
user1292656 Avatar asked Apr 20 '12 07:04

user1292656


People also ask

How do I select text in a drop down list?

We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.

How do you bind a selected item in a drop down list to a text box and edit it?

How to bind a selected item in a drop-down list to a text box and edit it? You can bind a selected value from the drop-down list using the ngModel and edit it inside the text box or numeric input box. The selected item text and value can be got using the change event of the drop-down list through the args. itemData.

How can you achieve the multiselect field under the form?

1. Find the form you want to add the multi-select field to, select it and click Edit Form. 2. Under Template Form Fields, find the field and drag it into the canvas.


2 Answers

drpFunction.SelectedValue = drpFunction.Items.FindByText(t).Value;

This is better way to select text. By ioden's way it will show an error

"Multiple Items Cannot be selected in DropDownList"

like image 63
Ms. Sonia Avatar answered Oct 04 '22 18:10

Ms. Sonia


 string t = "test";
 drpFunction.ClearSelection();
 drpFunction.Items.FindByText(t).Selected = true;
like image 33
ionden Avatar answered Oct 04 '22 19:10

ionden