Reputation: 45
I have a static drop-down list control in asp.net page and I have two different string (based on a result which I am fetching through database) now I want to make selected text of the the current input and the indexing and values remains same.
<asp:DropDownList ID="ddl_room_type" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_room_type_SelectedIndexChanged">
<asp:ListItem Selected="True">Select Type</asp:ListItem>
<asp:ListItem Text="Deluxe" Value="2500"></asp:ListItem>
<asp:ListItem Text="Super Deluxe" Value="3000"></asp:ListItem>
<asp:ListItem Text="Luxury" Value="5000"></asp:ListItem>
<asp:ListItem Text="Special Room" Value="10000"></asp:ListItem>
</asp:DropDownList>
and I have a string Text from Database : lets say Special Room. Now I want that drop-down list i.e, ddl_room_type should automatically bind with the same text and value.
C# Code which I am using:
string luxury = "Special Room"; //Value coming form Database
ddl_room_type.SelectedItem.Text = luxury;
If I do with this code.. it is just selected the first text as : "Special Room"
Upvotes: 1
Views: 1908
Reputation: 590
Alternately you can try this
string luxury = "Special Room";
ddl_room_type.ClearSelection();
ddl_room_type.Items.FindByText(luxury).Selected = true;
Hope it will be useful
Upvotes: 1
Reputation: 11787
Get your values from the database at first for both Text
and Value
for loading the dropdownlist
. So that you don't have to set it once the user makes the selection.
Upvotes: 0