如何为绑定列表框选择多个值 [英] how to select multiple values to binded listbox
本文介绍了如何为绑定列表框选择多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为绑定列表框选择多重值
我试过这个
foreach (DataRowView drv in SearchView)
{
LstBoxAuthors.SelectedIndex = LstBoxAuthors.Items.IndexOf( LstBoxAuthors.Items.FindByText(drv [ 1 ]。ToString()));
}
此代码选择最后一个值!
解决方案
< blockquote>检查这个 -
如何在ListBox中选择多个项目? [ ^ ]
您好!
查看我的解决方案。您应该能够或多或少地复制并粘贴它并根据您的需要进行调整:
标记:
<%@ 页 语言 = C# AutoEventWireup = true CodeBehind = ListBox.aspx.cs 继承 = ASPNET .ListBox %&g t;
< !DOCTYPE html >
< span class =code-keyword>< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = < span class =code-keyword> server >
< title > < / title >
< / head >
< 正文 >
< form id = form1 runat = 服务器 >
< div >
< h4 > DB中的作者< / h4 >
< asp:ListBox < span class =code-attribute> runat = server ID = AuthorsListBox 高度 = 200px 行 = 5 > < / asp:ListB ox >
< / div >
< div >
< h4 > 搜索< / h4 >
< 标签 > 搜索作者:< / label >
< asp:TextBox runat = server ID = SearchTextBox / >
< / div >
< div >
< asp:按钮 runat = server ID = SearchButton
文字 = 搜索 OnClick = SearchButton_Click / >
< asp:button runat = server ID = ResetSearchButton
< span class =code-attribute> < span class =code-attribute> 文字 = 清除 OnClick = ResetSearchButton_Click < span class =code-attribute> / >
< / div >
< div >
< h4 > 搜索结果< / h4 >
< asp: DataList runat = server ID = AuthorsDataList >
< ItemTemplate >
< label >
< span class =code-pagedirective>< ;% #Eval( AuthorName)%>
< / label >
< / ItemTemplate >
< / asp:DataList >
< / div >
< / form >
< / body >
< / html >
代码落后:
使用System;
使用System.Collections.Generic;
使用System.Data;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
命名空间ASPNET
{
公共部分类ListBox:System.Web.UI.Page
{
/// < 摘要 >
///本地数据源变量。
/// < / summary > ;
private List < 作者 > _Authors;
/// < 摘要 >
///获取或设置搜索文本框。
/// < / summary > ;
私有字符串_SearchBoxText
{
get {return SearchTextBox.Text; }
set {SearchTextBox.Text = value; }
}
/// < 摘要 >
///预加载数据。
/// < / summary > ;
/// < param name = sender > < / param >
/// < param 名称 = e > < < span class =code-leadattribute> / param >
protected void Page_ PreInit(对象发送者,EventArgs e)
{
_Authors = MockAuthors.GetMockAuthorData();
}
/// < 摘要 >
///设置控件。
/// < / summary > ;
/// < param name = sender > < / param >
/// < param 名称 = e > < < span class =code-leadattribute> / param >
protected void Page_加载(对象发送者,EventArgs e)
{
if(!Page.IsPostBack)
{
AuthorsListBox.DataSource = _Authors.Select(x => x.AuthorName);
AuthorsListBox.DataBind();
}
AuthorsListBox.SelectionMode = ListSelectionMode.Multiple;
}
/// < 摘要 >
///仅返回作者姓名的数组。
/// < / summary > ;
/// < 返回 > < / returns >
private IEnumerable < string > GetAuthorNames()
{
if(_Authors.Any())
{
return _Authors.Select(x => x。 AUTHORNAME);
}
返回新字符串[] {};
}
/// < 摘要 >
///重置新搜索的控件。
/// < / summary > ;
private void ResetSearch()
{
AuthorsListBox.SelectedIndex = -1;
_SearchBoxText = string.Empty;
AuthorsDataList.DataSource = null;
AuthorsDataList.DataBind();
SearchTextBox.Focus();
}
/// < 摘要 >
///执行作者姓名搜索。然后选择全局列表中的每个匹配数据列表项。
/// < / summary > ;
/// < param name = sender > < / param >
/// < param 名称 = e > < < span class =code-leadattribute> / param >
protected void Searc hButton_Click(object sender,EventArgs e)
{
//重置上次搜索中的控件。
AuthorsListBox.SelectedIndex = -1;
//搜索匹配搜索文本的作者
列出< 作者 > authorsMatchingSearch =
_Authors
.Where(x =>
x.AuthorName.ToLower()。包含( _SearchBoxText))
.ToList();
AuthorsDataList.DataSource = authorsMatchingSearch;
AuthorsDataList.DataBind();
//添加逻辑以便在找不到匹配项时执行某些操作。
//从主列表中选择作者
foreach(AuthorsListBox.Items中的ListItem项目)
{
if(authorsMatchingSearch
.Select(x = > x.AuthorName.ToLower())
.Contains(item.Text.ToLower()))
{
item.Selected = true;
}
}
}
/// < 摘要 >
///重置表单。
/// < / summary > ;
/// < param name = sender > < / param >
/// < param 名称 = e > < < span class =code-leadattribute> / param >
protected void重置SearchButton_Click(object sender,EventArgs e)
{
ResetSearch();
}
}
/// < 摘要 >
///表示作者实体的数据模型。
/// < / summary > ;
公共类作者
{
/// < 摘要 >
///作者的姓名。
/// < / summary > ;
公共字符串AuthorName {get;组; }
}
/// < 摘要 >
///生成模拟数据。
/// < / summary > ;
公共静态类MockAuthors
{
/// < 摘要 >
///模拟作者列表。
/// < / summary > ;
/// < 返回 > < / returns >
公共静态列表< 作者 > GetMockAuthorData()
{
返回新列表< 作者 >
{
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
}
};
}
}
}
这是成功的:
LstBoxAuthors.Items [LstBoxAuthors.Items.IndexOf(LstBoxAuthors.Items.FindByText(drv [ 1 ]。ToString() ))]。Selected = true ;
谢谢
how to select multible values to binded listbox
I tried this
foreach (DataRowView drv in SearchView)
{
LstBoxAuthors.SelectedIndex = LstBoxAuthors.Items.IndexOf(LstBoxAuthors.Items.FindByText(drv[1].ToString()));
}
this code select just last value!
解决方案
Check this -
How to Select multiple items in ListBox?[^]
Hi there!
Check out my solution. You should be able to more or less copy and paste it and adjust for your use:
Markup:
<%@ 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:
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" } }; } } }
It success with this:
LstBoxAuthors.Items[LstBoxAuthors.Items.IndexOf(LstBoxAuthors.Items.FindByText(drv[1].ToString()))].Selected = true;
Thanks
这篇关于如何为绑定列表框选择多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文