ARTICLE

Selecting an item in an ASP.NET DropDownList Programmatically

Posted by Mahesh Chand Articles | ASP.NET using VB.NET April 12, 2007
This tip shows how to select an item in an ASP.NET DropDownList control programmatically.
 
Reader Level:

The first method to select an item in an ASP.NET DropDownList control is using SelectedIndex property. To find the SelectedIndex of an item, you can use IndexOf method and pass use FindByValue method. Here is the code snippet to select item Mahesh" in the DropDownList.

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Mahesh"))

Alternatively, you can loop through all items of the DropDownList and compare the value of the item and use Selected property to select the matched item.

Private Function SelectCurrentQuarterAndYear(ByVal stringToSelect As String)
        DropDownList1.ClearSelection()
        For Each item As ListItem In DropDownList1.Items
            If item.Text = stringToSelect Then
                item.Selected = True
                Exit For
            End If
        Next
End Function

share this article :
post comment
 

Hi. Hows it going? ASP.NET usually incorporates view state by default; which means that data is preserved during round trips to the server (postback). I would assume that your problem is occuring because you are loading values into the dropdownlist box during the load event of the page. This can be solved by surrounding the code within this event handler with an if statement. I know that in C#, the code would be:

if (! IsPostback){ load the dropDownListBox here }

I am not sure what the VB code would be, but I am fairly sure that should solve your problem. Be in touch with an update. Take care.

Posted by Alex Rivenbark Oct 25, 2008

Hi, Currentlly i am working on a web application where i require to fill the web form with the selected item in a text box. for example i have dropdown control named supplier_name, and i want to fill all the other controls values from database on the form when i select name from that supplier_name box,i.e , if i select name1, then related to that name, its code,address ,city should appear on related text boxes. I have done the same in windows application with c# , but the problem with web application(asp.net) is that, when i select Supplier _ it reloads the page and i loose the values in that combo box...i have heard of postback method but i donno how to use it to implement the same. please any one send me answer

Posted by Bhushan Joshi Oct 24, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor