Wednesday, May 2, 2012

DropDownList FindByText

Default.aspx
---------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="ddl1" runat="server">
    <asp:ListItem>One</asp:ListItem>
    <asp:ListItem>Two</asp:ListItem>
    <asp:ListItem>Three</asp:ListItem>
    </asp:DropDownList>
    </div>
    </form>
</body>
</html>
Default.aspx.cs
---------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ListItem item = ddl1.Items.FindByText("Three");
        if (item != null)
        {
            item.Selected = true;
        }
    }
}



output:
--------