如何为绑定列表框选择多个值

编程入门 行业动态 更新时间:2024-10-26 20:34:05
本文介绍了如何为绑定列表框选择多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何为绑定列表框选择多重值 我试过这个

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

此代码选择最后一个值!

解决方案

检查这个 - 如何在ListBox中选择多个项目? [ ^ ]

您好! 查看我的解决方案。您应该能够或多或少地复制并粘贴它并根据您的需要进行调整: 标记:

<%@ 页 语言 = C# AutoEventWireup = true CodeBehind = ListBox.aspx.cs 继承 = ASPNET .ListBox %&g t; < !DOCTYPE html > < html xmlns = www.w3/1999/xhtml > < head runat = server > < title > < / title > < / head > < 正文 > < form id = form1 runat = 服务器 > < div > < h4 > DB中的作者< / h4 > < asp:ListBox 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 文字 = 清除 OnClick = ResetSearchButton_Click / > < / div > < div > < h4 > 搜索结果< / h4 > < asp: DataList runat = server ID = AuthorsDataList > < ItemTemplate > < label > < ;% #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 > < / param > protected void Page_ PreInit(对象发送者,EventArgs e) { _Authors = MockAuthors.GetMockAuthorData(); } /// < 摘要 > ///设置控件。 /// < / summary > ; /// < param name = sender > < / param > /// < param 名称 = e > < / 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 > < / 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 > < / 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="www.w3/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

更多推荐

如何为绑定列表框选择多个值

本文发布于:2023-07-26 09:33:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   绑定   何为   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!