Pergunta

I am making a website in asp.net and i have 2 list boxes:

lbxplayer1 and lbxplayer2

they have both been populated with the same amount of values and i would like it so that when one of the lists is clicked the other list will select the same item/index.

how do i do this? i assume the code would be in the lbxPlayer1_SelectedIndexChanged event?

So to build on what im trying to do, heres an example: lbxplayer1 has these values "bob" "jack" "Fred" lbxplayer2 has these values "dave" "Brian" "lee"

when i click on "jack" i want "Brian" to also be selected.

Foi útil?

Solução

I am not sure how you do populate your boxes, I mean manually or from a db table. you need ID for similar values, lets say we have lstBox1 and lstBox2, I will populate data on pageload, and will write code for selectedindexchanged:

      protected void Page_Load(object sender, EventArgs e)
{
    //disable code on postback
    if (IsPostBack) return;

    //creating first list items
    ListItem lst11 = new ListItem("Bob","1");
    ListItem lst12 = new ListItem("Jack", "2");
    ListItem lst13 = new ListItem("Fred", "3");

    //populating first list items
    lstBox1.Items.Add(lst11);
    lstBox1.Items.Add(lst12);
    lstBox1.Items.Add(lst13);

    //creating second list items
    ListItem lst21 = new ListItem("Dave", "1");
    ListItem lst22 = new ListItem("Brian", "2");
    ListItem lst23 = new ListItem("Lee", "3");

    //populating second list items
    lstBox2.Items.Add(lst21);
    lstBox2.Items.Add(lst22);
    lstBox2.Items.Add(lst23);
}
protected void lstBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    lstBox2.SelectedValue = lstBox1.SelectedValue;
}
protected void lstBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    lstBox1.SelectedValue = lstBox2.SelectedValue;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top