By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,070 Members | 1,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,070 IT Pros & Developers. It's quick & easy.

Get selected index of specific item before display

P: n/a
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.



Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
CmbTargetGroup.SelectedValue = 20
"martin" <ma***************@hotmail.com> wrote in message
news:Ox**************@TK2MSFTNGP12.phx.gbl...
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.



Nov 18 '05 #2

P: n/a
Bert

You can try

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

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

if(dropDown.Items[i].Value == databaseValue
dropDown.SelectedIndex = dropDown.Items.IndexOf(dropDown.Items[i])

}
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.