如何遍历asp.net中的列表框项目 [英] How to loop through a listbox items in asp.net

查看:49
本文介绍了如何遍历asp.net中的列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态填充的列表框,我想循环遍历它并一个接一个地检索它的项目。这就是我的尝试



i have a listbox populated dynamically, i want to loop through it and retrieve its items one after the other. this is what i have tried

foreach (object p in ListBox1.Items)
                    {
                        label1.Text = p.ToString();
                        MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = '"+ p.ToString() +"' ", conn);
                        MySqlDataAdapter ad = new MySqlDataAdapter();               
                        
                        ad.SelectCommand = cmd2;
                        DataTable dt = new DataTable();
                        ad.Fill(dt);
                        gridview1.DataSource = dt;
                        gridview1.DataBind();
                        Panel3.Visible = true;
                    }





但它不起作用。我需要帮助。谢谢



but it doesn't work. please i need assistance. Thanks

推荐答案

ListBox1.Items包含ListItem对象的集合。



我认为ToString()应该可行,但使用对象类型更安全,并根据您的需要获取文本或值:



ListBox1.Items contains a collection of ListItem objects.

I think ToString() should work, but it's safer to use the object type and get the text or value depending on what you need:

//you don't have to keep creating new commands.  just use a parameter.  It's safer anyway
MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = @code ", conn);
cmd.Parameters.Add(new SqlParameter("@code", SqlDbType.VarChar));
MySqlDataAdapter ad = new MySqlDataAdapter(); 

foreach (ListItem p in ListBox1.Items)
{
    label1.Text = p.Text;              
    
    cmd.Parameters["@code"] = p.Text;

    ad.SelectCommand = cmd2;
    DataTable dt = new DataTable();
    ad.Fill(dt);
    gridview1.DataSource = dt;
    gridview1.DataBind();
    Panel3.Visible = true;
}





尝试一下,看看它是否解决了这个问题^ _ ^



Andy



Try that and see if it fixes the issue ^_^

Andy


尝试以下代码:

Try below code:
foreach (ListItem li in listBox1.Items)
{
	if (li.Selected)
	{
		label1.Text = li.ToString();
		MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = '" + li.Value.ToString() + "' ", conn);
		MySqlDataAdapter ad = new MySqlDataAdapter();

		ad.SelectCommand = cmd2;
		DataTable dt = new DataTable();
		ad.Fill(dt);
		gridview1.DataSource = dt;
		gridview1.DataBind();
		Panel3.Visible = true;
	}
}



这里检查选择了哪些项目。根据所选项目,如果条件,它将进入内部。如果你想要你可以删除它。



注意:有一点我不明白为什么你在for循环中编写完整的代码库。假设我选择了3个项目,那么代码将执行3次,并将值设置为标签,Panel将显示为True。所以请将那些代码块放在forloop之外,你不想一次又一次地执行。


Here it is checking which items are selected. Based on selected item it will enter inside if condition. If you want you can remove it.

Note: One thing I am not getting why are you writing whole code base inside for loop. Suppose I selected 3 items then code will execute 3 times and it set value to label and Panel will be visible True. So please keep those code block outside forloop which you don't want to execute again and again.


MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = @cd ", conn);
                MySqlDataAdapter ad = new MySqlDataAdapter();
                DataTable dt = new DataTable();
                cmd2.Parameters.Add("@cd", MySqlDbType.VarChar);
                foreach (ListItem p in ListBox1.Items)
                {    
                    cmd2.Parameters["@cd"].Value = p.Text;
                    ad.SelectCommand = cmd2;

                    ad.Fill(dt);
                    gridview1.DataSource = dt;
                    gridview1.DataBind();
                    Panel3.Visible = true;         
                }                





这完美无缺



This worked perfectly


这篇关于如何遍历asp.net中的列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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