如何保存从下拉列表中选择并添加到列表框的多个数据 [英] how to save mutiple data selected from a dropdownlist and added to list box

查看:116
本文介绍了如何保存从下拉列表中选择并添加到列表框的多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请有人帮我这样做



有1个下拉列表,1个列表框和1个插入按钮。< br $>


下拉列表将包含所有选项,如果用户选择一个并按下插入按钮,它将被插入列表框中,如果用户想要插入另一个项目,它将再次插入列表框。



然后让我们说列表框现在包含用户选择的所有项目。



如果点击了保存按钮,它将全部保存到数据库中,列名为已插入。





我设法完成了从下拉列表中选择项目到列表框的第一部分。

但是,我在将数据从列表框(多个数据)保存到数据库时遇到了麻烦。 br $> b $ b



请有人帮帮我....





Hi,
Please can anyone help me in doing this

There is 1 dropdownlist and 1 listbox and 1 insert button.

dropdownlist will contain all the choices and if user choose one and hit the insert button it will be inserted in the listbox, and if user would like to insert another item it will be inserted again in the listbox.

and then lets say that the listbox now contain all of the items chosen by the user.

if save button was clicked it will all be saved into the database with the column name "Inserted".


I managed to finish the first part which is selecting items from dropdownlist into the list box.
But, I am having trouble in saving the data from list box(multiple data) to the database.


Please can someone help me....


protected void usersavebtn_Click(object sender, EventArgs e)
        {

            string connectionString = ConfigurationManager.AppSettings["ApplicationServices"];
            SqlConnection myconnection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("sp_InsertAdmin_User", myconnection);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@Userid", Convert.ToInt32(usersddl.SelectedValue));
            cmd.Parameters.AddWithValue("@Tier", userbranchgrplistbox.SelectedValue);
            cmd.Parameters.AddWithValue("@Branch", userbranchlistbox.SelectedValue);
            cmd.CommandType = CommandType.StoredProcedure;
            myconnection.Open();
            cmd.Connection = myconnection;
            cmd.ExecuteNonQuery();
            myconnection.Close();

        }     





以上是我试过的代码...



Tier下拉列表是文本,但我必须将其id存储在数据库中..



above is the code i have tried...

Tier dropdown is text but i have to store its id in the database..

推荐答案

考虑将您的数据存入(独立)结构化数据阵列,该结构的一个字段是选定的字段。现在,在选择(或取消选择)项目时,您只需更改阵列上的标志并处理填充下拉列表和列表框。列表框和下拉列表只需要包含可见部分和引用。



我已成功使用此功能,即使对于无状态数据,例如一个网页(javaScript处理可见性,php处理数据库的结局)。



此时,您可以将数据输入数据库通过数据阵列上的循环。您的数据数组可以包含结构中任何数量和多样的数据。您可以简单地循环遍历数组,并根据需要对选定元素和非选定元素进行操作。



大部分可以通过控制结构中的存储来完成,但是访问普通数组中的数据(或者如果你愿意,可以访问ArrayList)要容易得多。 br />


你的存储阵列结构看起来像{userid,tier,branch,isSelected},让我再强调只有一个数组包含你想要使用的所有数据在你的数据传输中。
Consider having your data in (an independent) structured data array, one field of the structure being a ''selected'' field. Now, as items are selected (or de-selected), you need only change the flag on your array and handle populating your droplist and listbox. The list box and drop-list only need contain the visible portion and a reference.

I''ve used this successfully even for stateless data, such as an web page (javaScript handles the visibility, php handles the finale to the database).

At this point, you can enter you data into the database via a loop on your data array. Your data array can contain any amount and diversity of data in the structure. You can simply loop through your array and operate on the selected vs. non-selected elements as you like.

Much of this could be done via storage in the control structure, instead, but accessing data in a normal array (or ArrayList if you prefer) is much easier.

Your storage array structurce can look like {userid, tier, branch, isSelected}, and let me reemphasize that there is only a single array containing all data you wish to use in your data transfer.


这通常是.NET - >我更喜欢C ++,你似乎在使用C#。以下介于两者之间。原理是一样的



public struct youList {

int userid;

int tier;

int branch

bool isSelected;

};



ArrayList dataSet = new ArrayList() ;



//加载数组列表:

foreach(yourData as d){

struct youList =新的; //带有数据的新对象

y.userid = d.userid; //填写你的数据

......等等......

y.isSelected = false; //如果你从现有的dbase数据加载,这可能是d.isSelected



dataSet.Add(y)

}



//从这一点开始,你遍历列表并且

//根据
$ b将数据添加到你的下拉列表或listBox中$ b // isSelected是真还是假;对于

//加载上一个会话很有用。



/ *现在,当从下拉列表中选择一个项目时,你

1 - 获取它的唯一标识符(userid?)

2 - 在数据集中将isSelected设置为true。

3 - 删除来自droplist

4 - 添加到listBox

可选择,你可以清除两个控件和

根据isSelected标志重新填充它们

而不是3,4以上



转移到数据库时,只需循环数组

并使用适当的SQL插入/更新/删除项目

基于用户ID并被选中。



有很多变种可以提高效率

针对具体情况。 ArrayList是一种方便的方式,以一致的方式处理数据,并允许您创建

泛型方法来处理这个任务中的任何选择例程。 $ b任何类型的控制方案。

* /
This is generically .NET -> I prefer C++, you seem to be using C#. The following is somewhere in between. Principle is the same

public struct youList {
int userid;
int tier;
int branch
bool isSelected;
};

ArrayList dataSet = new ArrayList();

// Load your array list:
foreach (yourData as d) {
struct youList = new y; // new object with you data
y.userid = d.userid; // fill w/your data
... etc ...
y.isSelected = false; // this may be d.isSelected if you load from existing dbase data

dataSet.Add(y)
}

// From this point, you iterate through the list and
// Add data to your drop list OR listBox based upon
// whether isSelected is true or false; Useful for
// loading a previous session.

/* Now, when an item is selected from the drop list you
1 - get it''s unique identifier (userid?)
2 - set isSelected to ''true'' in dataset.
3 - remove from droplist
4 - add to listBox
Optionally, you could clear both controls and
repopulate them based upon the isSelected flag
instead of 3, 4 above

When transferring to database, simply loop the array
and use appropriate SQL to insert/update/delete items
based upon userID and isSelected.

There are many variations on this to improve efficiency
for specific situations. The ArrayList is a handy way to
handle data in a consistent manner and allow you to create
generic methods to handle this for any select routine in
any type of control scenario.
*/


string selectedIDs = string.Empty;
if (lstBox.Items != null && lstBox.Items.Count > 0)
{
   foreach (ListItem item in lstBox.Items)
   {
       selectedIDs += item.Value + ",";
   }
   selectedIDs = selectedIDs.Trim();
}







这里你会得到像1,2,3,4这样的数据。



因此,在存储过程中,您可以拆分逗号分隔值并在存储过程中逐个插入。




here you will get data like 1,2,3,4.

So in your stored procedure you can split comma separated values and insert one by one in stored procedure.


这篇关于如何保存从下拉列表中选择并添加到列表框的多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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