SelectedIndex in DropDownList

Results 1 to 3 of 3

Thread: SelectedIndex in DropDownList

  1. #1
    Join Date
    Dec 1969

    Default SelectedIndex in DropDownList

    I am trying to setup a Selected Indexd in a DropDownList. Below is my function that loops through a dataset looking for a match. Does anyone see anything drastically wrong with it? If you need to see other aspects, please let me know.<BR><BR><BR> Public Overloads Shared Function fnGetPMInformationTypeIDIndex(ByVal lngPMInformationTypeID As Integer) As Integer<BR> Dim dsProductMarketing As DataSet = SqlHelper.ExecuteDataset(ConfigurationSettings.App Settings(Web.Global.CfgKeyConnString), "spGetPMInformationTypes")<BR> Dim iIndex As Integer<BR><BR> For iIndex = 0 To dsProductMarketing.Tables(0).Rows.Count - 1<BR> If Convert.ToInt32(lngPMInformationTypeID) = Convert.ToInt32(dsProductMarketing.Tables(0).Rows( iIndex).Item("lngPMInformationTypeID")) Then<BR> Return iIndex<BR> End If<BR> Next iIndex<BR> End Function &#039;PMInformationTypeIDIndex

  2. #2
    Join Date
    Dec 1969

    Default RE: SelectedIndex in DropDownList

    &#039;example: I want the state that is stored for my address<BR>to be the selected state in the dropdownlist for state<BR>----------<BR><BR>One solution is to create the dropdown first, then set the selected based on the row in the dropdownlist that matches the field<BR>&#039;After you bind your dataset to your dropdownlist<BR>&#039;ddl - dropdownlist to be searched<BR>&#039;strSelected - value to match to set as selected item in dropdownlist<BR><BR><BR>Sub SetDDL(ddl,strSelected)<BR> Dim i As Integer<BR> &#039;loop through dropdownlist(dataset)<BR> For i = 0 To ddl.Items.Count - 1<BR> &#039;if the dropdownlist match set as selected item<BR> If ddl.Items(i).Value = strSelected<BR> ddl.SelectedIndex = i<BR> End If<BR> Next<BR>End Sub<BR><BR><BR>T

  3. #3

    Default ARRRGH RE: SelectedIndex in DropDownList

    There are built in methods for this!<BR>Find by value and Find by text...<BR><BR>DropDownList1.DataSource = SqlHepers.ExecuteDataSet(....);<BR>DropDownList1.D ataBind();<BR>//testing to be sure it was found<BR>ListItem temp = DropDownList1.Items.FindByText("a value");<BR>temp.Selected=true;<BR>//no test<BR>DropDownList1.Items.FindByValue("a value").Selected=true;<BR><BR>p.s. Your SetDDL Sub will process all 500 items in the list even if the first item is the one to select...<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts