简体   繁体   中英

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 . 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.

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

       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

Since you want to show DataTextField value and DataValueField value from one listbox into 2 different listbox. 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();
}*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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