列表框中的多个选定值 [英] listbox multiple selected values

查看:87
本文介绍了列表框中的多个选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议代码将列表框中的多个选定值存储到asp.net中的数据库c#?

can anyone suggest the code to store multiple selected values from listbox to database in asp.net c# ?

推荐答案

或类似的东西:



Or something like this:

foreach (object item in listbox.SelectedItems)
{
    // do domething
}





在我的情况下,我会把一个属性放入item对象,它返回对象的XML表示形式,并将其传递给数据库。这样,上面显示的循环看起来像这样:





In my case, I'd put a property into the "item" object that returns an XML representation of the object, and pass that to the database. That way, the loop shown above would look something like this:

XElement root = new XElement("root");
foreach (object item in listbox.SelectedItems)
{
    XElement node = (item as MyItemType).AsXElement;
    root.Add(node);
}
// pass the root element as a string to your stored proc





看看它是多么干净整洁?



See how clean and tidy that is?


这个链接这个

something link this
if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    string selectedItem = ListBox1.Items[i].Text;
                    //insert command
                }
            }
        }


它取决于你的数据库设计。您可以通过多种方式存储数据。

单向,您可以根据选择放置多行。

或者您也可以使用逗号之类的分隔符保存它单行。



获取所选项目迭代列表框中的所有项目,并使用listitem的selected属性查找所有选择的元素。现在根据您的设计,您可以逐个保存,一次性将其插入数据库。
Its upto your design of database. You can store the data in multiple ways.
One way , you can put the multiple rows according to the selection.
Or you can also save it using some separator like comma in a single row.

To get selected items Iterate all the items in listbox and find all the seleced elements using the selected property of listitem. Now according to your design, you can save one by one, insert it in database in one go.


这篇关于列表框中的多个选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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