How to select an item in a DropDownList by value Asp.Net
Back to learning
Created: 31/10/2013
Software: Visual Studio 2012, fwk 3.5
Code i used.
Asp.Net
C#
1) Add dropdownlist to your website
and in code-behind try to select some item by using FindByValue
This is a result:
2) But what about if i whant to set previously selection of any item in html like this example:
I will recive: You cant select more than one item.
To solve this issue just add ClearSelection before your assignment like this:
And the result
That's all, hope this helps somebody.
How to select an item in a DropDownList by value Asp.Net
Software: Visual Studio 2012, fwk 3.5
Code i used.
Asp.Net
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddListTest" runat="server">
<asp:ListItem Value="item1" Selected="True">Item 1</asp:ListItem>
<asp:ListItem Value="item2">Item 2</asp:ListItem>
<asp:ListItem Value="item3">Item 3</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
C#
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ddListTest.ClearSelection();
ddListTest.Items.FindByValue("item2").Selected = true;
}
}
1) Add dropdownlist to your website
and in code-behind try to select some item by using FindByValue
This is a result:
2) But what about if i whant to set previously selection of any item in html like this example:
I will recive: You cant select more than one item.
To solve this issue just add ClearSelection before your assignment like this:
And the result
That's all, hope this helps somebody.