Click here to Skip to main content
15,913,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to select multible values to binded listbox
I tried this

C#
foreach (DataRowView drv in SearchView)
            {
  LstBoxAuthors.SelectedIndex = LstBoxAuthors.Items.IndexOf(LstBoxAuthors.Items.FindByText(drv[1].ToString()));
            }


this code select just last value!
Posted
Updated 7-Jun-15 14:25pm
v2

 
Share this answer
 
Hi there!
Check out my solution. You should be able to more or less copy and paste it and adjust for your use:

Markup:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs" Inherits="ASPNET.ListBox" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h4>Authors in DB</h4>
            <asp:ListBox runat="server" ID="AuthorsListBox" Height="200px" Rows="5"></asp:ListBox>
        </div>
        <div>
            <h4>Search</h4>
            <label>Search Author:</label>
            <asp:TextBox runat="server" ID="SearchTextBox" />
        </div>
        <div>
            <asp:Button runat="server" ID="SearchButton"
                Text="Search" OnClick="SearchButton_Click" />
            <asp:button runat="server" ID="ResetSearchButton"
                Text="Clear" OnClick="ResetSearchButton_Click" />
        </div>
        <div>
            <h4>Search Results</h4>
            <asp:DataList runat="server" ID="AuthorsDataList">
                <ItemTemplate>
                    <label>
                        <%#Eval("AuthorName") %>
                    </label>
                </ItemTemplate>
            </asp:DataList>
        </div>
    </form>
</body>
</html>


Code behind:

XML
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPNET
{
    public partial class ListBox : System.Web.UI.Page
    {
        /// <summary>
        /// A local data source variable.
        /// </summary>
        private List<Author> _Authors;

        /// <summary>
        /// Gets or sets the search text box.
        /// </summary>
        private string _SearchBoxText
        {
            get { return SearchTextBox.Text; }
            set { SearchTextBox.Text = value; }
        }

        /// <summary>
        /// Pre load data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_PreInit(object sender, EventArgs e)
        {
            _Authors = MockAuthors.GetMockAuthorData();
        }

        /// <summary>
        /// Set controls.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AuthorsListBox.DataSource = _Authors.Select(x => x.AuthorName);
                AuthorsListBox.DataBind();
            }
            AuthorsListBox.SelectionMode = ListSelectionMode.Multiple;
        }

        /// <summary>
        /// Returns only an array of the author names.
        /// </summary>
        /// <returns></returns>
        private IEnumerable<string> GetAuthorNames()
        {
            if(_Authors.Any())
            {
                return _Authors.Select(x => x.AuthorName);
            }
            return new string[] { };
        }

        /// <summary>
        /// Resets controls for new search.
        /// </summary>
        private void ResetSearch()
        {
            AuthorsListBox.SelectedIndex = -1;
            _SearchBoxText = string.Empty;
            AuthorsDataList.DataSource = null;
            AuthorsDataList.DataBind();
            SearchTextBox.Focus();
        }

        /// <summary>
        /// Performs search of author names. Then selects each matching data list item in global list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            // Reset the controls from the previous search.
            AuthorsListBox.SelectedIndex = -1;

            // Search authors with matching search text
            List<Author> authorsMatchingSearch =
                _Authors
                .Where(x =>
                    x.AuthorName.ToLower().Contains(_SearchBoxText))
                .ToList();
            AuthorsDataList.DataSource = authorsMatchingSearch;
            AuthorsDataList.DataBind();

            // Add logic to do something when no matches found.

            // Select the authors from main list
            foreach (ListItem item in AuthorsListBox.Items)
            {
                if (authorsMatchingSearch
                    .Select(x => x.AuthorName.ToLower())
                    .Contains(item.Text.ToLower()))
                {
                    item.Selected = true;
                }
            }
        }

        /// <summary>
        /// Resets the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ResetSearchButton_Click(object sender, EventArgs e)
        {
            ResetSearch();
        }
    }

    /// <summary>
    /// A data model representing an Author entity.
    /// </summary>
    public class Author
    {
        /// <summary>
        /// The name of the Author.
        /// </summary>
        public string AuthorName { get; set; }
    }

    /// <summary>
    /// Generates mock data.
    /// </summary>
    public static class MockAuthors
    {
        /// <summary>
        /// Mocks up Authors List.
        /// </summary>
        /// <returns></returns>
        public static List<Author> GetMockAuthorData()
        {
            return new List<Author>
            {
                new Author
                {
                    AuthorName = "James Smith"
                },
                new Author
                {
                    AuthorName = "Sally Cunnigham"
                },
                new Author
                {
                    AuthorName = "Billy Buffinger"
                },
                new Author
                {
                    AuthorName = "James Jones"
                },
                new Author
                {
                    AuthorName = "James Cunningham"
                }
            };
        }
    }
}
 
Share this answer
 
It success with this:

C#
LstBoxAuthors.Items[LstBoxAuthors.Items.IndexOf(LstBoxAuthors.Items.FindByText(drv[1].ToString()))].Selected = true;


Thanks
 
Share this answer
 

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