如何从数据库中突出显示列表框中已选择的项目 [英] how to highlight already selected items in listbox from database

查看:127
本文介绍了如何从数据库中突出显示列表框中已选择的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我的列表框选择项与关联存储在数据库中,现在我要更新表单,希望以高亮度进行选择.你可以指导我还是发送任何摘录

hello ,
my listbox selected item are astored in database as concatened with , now i want update my form i want to get selected with highligthing can u guide me or send any snipets

推荐答案

使用事件数据绑定事件 [
Use event DataBound Event[^]

OnDataBound event, check if the value was earlier selected - if so, apply different style to that listitem. Define a Databound handler for the ListBox. During databind, the execution will go through this method then.

Something like:
protected void lstMultipleValues_DataBound(object sender, EventArgs e)
{
  foreach (ListItem item in lstMultipleValues.Items)
  {
    //check anything out here
    if (item.Text.StartsWith("B"))
      item.Attributes.Add("style", "color:red");
  }
}


如果您尝试手动执行此操作,并且没有数据绑定,则如下所示:

If you''re trying to manually do it, and are not databound, something like this:

<asp:listbox id="list1" runat="server" selectionmode="Multiple" xmlns:asp="#unknown">
    <asp:listitem value="1" text="Apple"></asp:listitem>
    <asp:listitem value="2" text="Orange"></asp:listitem>
    <asp:listitem value="3" text="Banana"></asp:listitem>
    <asp:listitem value="4" text="Pear"></asp:listitem>
</asp:listbox>



背后的代码:



Codebehind:

// Selections coming from your database
string selections = "2,4";  

// Create array of selected values
string[] selectedValues = selections.Split('','');

// Make sure there can be multiple value selected on you list box
list1.SelectionMode = ListSelectionMode.Multiple;

foreach (string value in selectedValues)
{
    ListItem item = list1.Items.FindByValue(value);
    if (item != null)
    {
	item.Selected = true;
    }
}


这篇关于如何从数据库中突出显示列表框中已选择的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆