ASP.NET - how to find value and data in listbox

Asked By msakt on 04-Jun-12 03:45 AM
how to find value and data in listbox
Ravi Maurya replied to msakt on 04-Jun-12 03:52 AM
hey, please refer this article 

http://asp-net-example.blogspot.in/2009/03/how-to-find-list-item-by-text-in.html


hope this might help you
Jitendra Faye replied to msakt on 04-Jun-12 04:33 AM
Try this code-


 ListItem item =  ListBox1.Items.FindByValue("searchvalue");
DL M replied to msakt on 04-Jun-12 04:46 AM
Hi..

Refer this link

http://stackoverflow.com/questions/9211094/how-to-get-the-selected-value-field-of-listbox-vb-net

will help you..!!
dotnet freak replied to msakt on 04-Jun-12 05:23 AM
Please refer this example:

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    { 
       string searchText = TextBox1.Text.ToString();
       if (ListBox1.Items.FindByValue(searchText) != null)
       {
           Label1.Text = "Item Found, Value: " + searchText;
           ListBox1.Items.FindByValue(searchText).Selected = true;
       }
       else
       {
           Label1.Text = "Item not Found, Value: " + searchText;
       }
    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to find list item by value in ListBox, FindByValue()</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">ListBox example: Find By Value</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="Teal"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="asp.net controls"
             Font-Bold="true"
             ForeColor="DarkGreen"
             >
        </asp:Label>
        <br />
        <asp:ListBox 
             ID="ListBox1"
             runat="server"
             BackColor="DarkGreen"
             ForeColor="FloralWhite"
             >
             <asp:ListItem Value="1">ImageMap</asp:ListItem>
             <asp:ListItem Value="2">Literal</asp:ListItem>
             <asp:ListItem Value="3">ListView</asp:ListItem>
             <asp:ListItem Value="4">RegularExpressionValidator</asp:ListItem>
             <asp:ListItem Value="5">ValidationSummary</asp:ListItem>
             <asp:ListItem Value="6">Menu</asp:ListItem>
        </asp:ListBox>
        <br /><br />
        <asp:Label 
             ID="Label3" 
             runat="server"
             ForeColor="SeaGreen"
             Text="Item Text"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="SeaGreen"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Find Item"
             Font-Bold="true"
             ForeColor="SeaGreen"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>

RAJASEKHAR RAJENDRAN replied to msakt on 04-Jun-12 06:59 AM
hi msakt,

If you need tp Get the specific items you can achieve using the below.

string item = lstItems.Items[index].toString(); 

if you need to get all the items in a list, you can achieve using the below.

var myList = lbMyListBox.Items.Cast<String>().ToList();

Have a look at the below links, which might help 

http://asp-net-example.blogspot.in/2009/03/how-to-find-list-item-by-value-in.html 
http://www.digitalwatermelon.com/1/setting-listbox-selectedvalues-in-asp-net/ 


Thanks & Regards,
Rajasekhar.R