Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of items in the asp list box, and I am passing a value which is listed in the list box, then that item wants to be selected in the list box.

Please help me how to do this.

Advance thanks for helping..
Posted
Updated 14-Sep-12 5:00am
v2
Comments
[no name] 14-Sep-12 10:25am    
What have you tried?

XML
<asp:ListBox ID="ListBox1" runat="server">
            <asp:ListItem Value="1">Add</asp:ListItem>
            <asp:ListItem Value="2">Sub</asp:ListItem>
            <asp:ListItem Value="3">Mul</asp:ListItem>
            <asp:ListItem Value="4">Div</asp:ListItem>
        </asp:ListBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />




C#
protected void Button1_Click(object sender, EventArgs e)
        {
            ListItem l = new ListItem("Sub", "2");
            ListBox1.SelectedIndex = ListBox1.Items.IndexOf(l);
        }



Hope this code may help you.....
 
Share this answer
 
Hello,

If you need to select "Libish" from the list box, use the following code.

XML
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:ListBox ID="ListBox1" runat="server">
        <asp:ListItem>vinod</asp:ListItem>
        <asp:ListItem>Alex</asp:ListItem>
        <asp:ListItem>Libish</asp:ListItem>
    </asp:ListBox>

protected void Button1_Click(object sender, EventArgs e)
{
        ListBox1.SelectedValue = "Libish";
}


Thanks!!!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900