Set dropdownlist value

  • Thread starter Thread starter tfs
  • Start date Start date
T

tfs

I have a dropdownlist that I was setting to an index (which happened
to be the same as the value). I changed this to a character as that
is what I want to have in my database.

<asp:dropdownlist
style="border:none" id="recurrenceType"
runat="server">
<asp:listitem value="N"
None</asp:listitem> <asp:listitem value="M"
Monthly</asp:listitem> <asp:listitem value="P"
Periodic</asp:listitem>
</asp:dropdownlist>


I was setting the value before (when the value was "0", "1" and "2").
It used to work like this:

recurrenceType.SelectedIndex = objDataReader("recurrenceType")

How would I set it with the value set to a char?

Thanks,

Tom.
 
I have tried all kinds of ways to get this to work and keep getting
strange (at least to me) results.

If I do a viewsource of the above statement (my last post):

recurrenceType.SelectedItem.value = "P"

I get the following:

<select name="recurrenceType" id="recurrenceType"
style="border:none">
<option selected="selected" value="P">None</option>
<option value="M">Monthly</option>
<option value="P">Periodic</option>
</select>

It seems to change the value, which I guess is what I am saying - but
this is how I saw it selected in one example. I am trying to get it
to move the selected="selected" to the 3rd option and it isn't doing
it.

I then tried:

recurrenceType.Items.FindByValue("P").selected = true

And got the following error:

A DropDownList cannot have multiple items selected.
Description: An unhandled exception occurred during the execution
of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A DropDownList
cannot have multiple items selected.

Source Error:


Line 132: </table>
Line 133:
Line 134: <form runat="server">
Line 135: <table align="center">
Line 136: <tr
valign="baseline">


This is driving me crazy.

Thanks,

Tom.
 
I just tried this:

recurrenceType.SelectedItem.value = "P"

The display on the screen is still "None".

Thanks,

Tom.
 
The syntax should be something close to this:

recurrenceType.SelectedItem=recurrenceType.Items.FindByValue("P")

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net



tfs said:
I have a dropdownlist that I was setting to an index (which happened
to be the same as the value). I changed this to a character as that
is what I want to have in my database.

<asp:dropdownlist
style="border:none" id="recurrenceType"
runat="server">
<asp:listitem value="N"
None</asp:listitem> <asp:listitem value="M"
Monthly</asp:listitem> <asp:listitem value="P"
Periodic</asp:listitem>
</asp:dropdownlist>


I was setting the value before (when the value was "0", "1" and "2").
It used to work like this:

recurrenceType.SelectedIndex = objDataReader("recurrenceType")

How would I set it with the value set to a char?

Thanks,

Tom.
 
SelectValue was it.

Here is what I did that works fine.


recurrenceType.SelectedValue =
objDataReader("recurrenceType")
recurrenceDisplay.text = recurrenceType.SelectedItem.text

<td nowrap align="right">Recurrance:</td>
<td>
<asp:label id="recurrenceDisplay"
runat="server" />
<asp:dropdownlist style="visibility:hidden"
id="recurrenceType" runat="server">
<asp:listitem value="N"
None</asp:listitem> <asp:listitem value="M"
Monthly</asp:listitem> <asp:listitem value="P"
Periodic</asp:listitem>
</asp:dropdownlist>
</td>


Thanks,

Tom
 
Back
Top