Get selected index of specific item before display

  • Thread starter Thread starter martin
  • Start date Start date
M

martin

Hi,

I have a dropdown control tha is rendered with the follwoing HTML

<select name="FirstStep:CmbTargetGroup" id="FirstStep_CmbTargetGroup"
style="width:152px;">
<option value="1">Martin</option>
<option value="2">Chris</option>
<option value="5">adrian</option>
<option value="7">Tony</option>
<option value="8">Dave</option>
<option value="15">stewart</option>
<option value="20">Bert</option>
</select>

the above values are taken straight from a database and bound to the
control.
Note that the value option does not indicate the selected index.

Once the items are bound to the control I wish to select "Bert" with a value
of 20 to be display in the dropdown box.
ie. the value in the dropdown box is initially set to "bert"

I know the value is 20, but do not know the value of the selected index (in
advance) to set it to before hand.

I would like to ask the best way to set the drop down box to the value of
"bert" after the items are bound to the control but before the page is
displayed.

many thanks in advance.

martin.
 
Bert

You can try

// C
public void SetDropDownIndex(DropDownList dropDown, string databaseValue

for(int i=0; i < dropDown.Items.Count; i++

if(dropDown.Items.Value == databaseValue
dropDown.SelectedIndex = dropDown.Items.IndexOf(dropDown.Items)

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top