简体   繁体   English

如何在asp.net中的不同列表框中显示所选项目的datavaluefield

[英]how to display the datavaluefield of selected items in different listbox in asp.net

I have 3 listboxes.The listbox fetches two values by code-datatextfield and datavaluefield . 我有3个列表框。列表框通过code-datatextfielddatavaluefield获取两个值。 1st listbox transfers the datatextfield items from 1st listbox to 2nd listbox.i want to transfer the datavaluefield of selected 1st listbox items to 3rd listbox. 第一个列表框将datatextfield项目从第一个列表框传输到第二个列表框。我想将所选的第一个列表框项目的datavaluefield传输到第三个列表框。

if (ListBox3.SelectedIndex >= 0
{
   for (int i = 0; i < ListBox3.Items.Count; i++)
    {
       if (ListBox3.Items[i].Selected)
       {
            if (!arraylist1.Contains(ListBox3.Items[i]))
            {
                arraylist1.Add(ListBox3.Items[i]);
                Session["value"] = ListBox3.Items[i];
            }
        }
    }
    for (int i = 0; i < arraylist1.Count; i++)
    {
        if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
        {
            ListBox2.Items.Add(((ListItem)arraylist1[i]));

        }
        ListBox3.Items.Remove(((ListItem)arraylist1[i]));
    }
    ListBox2.SelectedIndex = -1;

}
else
{

}



SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from IT_1_BOILER_DESK_1_PARAMETERS where paramtext='" + Session["value"] + "'", con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
    ListBox1.DataSource = ds.Tables[0];
    ListBox1.DataTextField = "param";
    //ListBox3.DataValueField = "param";
    ListBox1.DataBind();
}

for listbox 3 用于列表框3

       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(strcon);
    SqlCommand command = new SqlCommand("select * from IT_1_BOILER_DESK_1_PARAMETERS where pname='" + this.DropDownList1.SelectedValue + "'" ,con);
    SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    dataAadpter.Fill(ds);
    if (ds != null)
    {
        ListBox3.DataSource = ds.Tables[0];
        ListBox3.DataTextField = "paramtext";
        ListBox3.DataValueField = "param";
        ListBox3.DataBind();


    }
}

But i want to display the datavaluefield of the items that are selected from listbox3 to listbox1 但是我想显示从listbox3到listbox1选择的项目的datavaluefield

Since you want to show DataTextField value and DataValueField value from one listbox into 2 different listbox. 由于要显示DataTextField值和DataValueField值从一个列表框到2个不同的列表框。 I suggest you to use a different approach. 我建议您使用其他方法。 This will also reduce usage of 2nd database call. 这也将减少第二数据库调用的使用。

Try following:- 请尝试以下:

if (ListBox3.SelectedIndex >= 0)
{
   for (int i = 0; i < ListBox3.Items.Count; i++)
    {
       if (ListBox3.Items[i].Selected)
       {
            if (!arraylist1.Contains(ListBox3.Items[i]))
            {
                arraylist1.Add(ListBox3.Items[i]);
                //Session["value"] = ListBox3.Items[i]; no need of this
            }
        }
    }
    for (int i = 0; i < arraylist1.Count; i++)
    {
        if (ListBox2.Items.FindByText(((ListItem)arraylist1[i]).Text)==null)
        {
//since you already have text and value field values in arrayList. Use them
            ListBox2.Items.Add(new ListItem(((ListItem)arraylist1[i]).Text));

        }
        if (ListBox1.Items.FindByText(((ListItem)arraylist1[i]).Text)==null) 
{
ListBox1.Items.Add(new ListItem((ListItem)arraylist1[i]).Value));
}

ListBox3.Items.Remove(((ListItem)arraylist1[i]));
    }
    ListBox2.SelectedIndex = -1;

}
else
{

}

/* This database call can be removed

SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from IT_1_BOILER_DESK_1_PARAMETERS where paramtext='" + Session["value"] + "'", con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
    ListBox1.DataSource = ds.Tables[0];
    ListBox1.DataTextField = "param";
    //ListBox3.DataValueField = "param";
    ListBox1.DataBind();
}*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM