Go to Comments





How to select an item in a DropDownList by value Asp.Net


Created: 31/10/2013 07:56:09 p.m.





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.






Comments:

User: Bhuva Bhavesh Comment date: 21/05/2014 01:24:36 a.m.
very useful
User: sadegh Comment date: 05/10/2014 03:29:23 a.m.
Tanks - its very help me in one Problem
:)
User: fery Comment date: 04/02/2015 10:29:05 a.m.
 this help for me . thx a lot
User: lina Comment date: 25/02/2015 04:09:52 a.m.
please send us the code for sending mail in asp.net(not c#)
User: Docstorus Comment date: 25/02/2015 06:20:02 p.m.
you meen this one?
http://www.docstorus.com/viewer.aspx?code=34fd84cc-6f0d-468f-aa79-62ad38775da2