I’m not being able to capture the various items selected in Lect2, how to do it?

Asked

Viewed 138 times

0

I’m not being able to capture the various items selected in Lect2, how to do it? My goal will be to capture these items and send them to a Textbox, which I am using ?

A list, see below:

<asp:ListBox id="lb" 
         runat="server" 
         class="form-control input-sm select2-multiple" 
         Width="70%" 
         multiple>
    <asp:ListItem Value="1">Administração</asp:ListItem>
    <asp:ListItem Value="2">Administração Pública</asp:ListItem>
    <asp:ListItem Value="3">Agronegócios e Agropecuária</asp:ListItem>
</asp:ListBox>

Like I’m trying to do ?

protected void Btn_Teste_Click(object sender, EventArgs e)
{     
    string message = "";
    foreach (ListItem item in lb.Items)
    {
        if (item.Selected)
        {
           message += item.Text ;
        }
    }
    TextBox.Text = message;
}

The way I’m doing is only returning the first selected item?

  • The way I’m doing is only returning the first selected item

  • Take a look at the tour of the site

2 answers

0

Wrong configured component, not a common attribute, is a property with the name of SelectionMode where your configuration pattern is Single but, it needs to be Multiple so that the selection is multiple (multiple).

SelectionMode="Multiple"

Follow the full example:

<asp:ListBox
    ID="lb"
    runat="server"
    class="js-example-basic-single"
    SelectionMode="Multiple">
    <asp:ListItem Value="1">Administração</asp:ListItem>
    <asp:ListItem Value="2">Administração Pública</asp:ListItem>
    <asp:ListItem Value="3">Agronegócios e Agropecuária</asp:ListItem>
</asp:ListBox>      

Bonus:

To recover the values and even the text follows the code example:

// Recupera os indices selecionados
int[] indices = lb.GetSelectedIndices();
foreach (int indice in indices)
{
    //recupera o texto 
    lb.Items[indice].Text
    //recupera o valor
    lb.Items[indice].Value; 
}

the method GetSelectedIndices() recovers the indexes that are selected and in order to recover the selected values you need to access indice of the method Items and in it has property Text (which is the value of the selected box) and Value (which is the value of the selected box), I find it more performative for not having to sweep all your items. The shape that is in your question works, but, performance is compromised by having obligation to pass on all items.


References:

  • So friend dev as I am declaring and calling the list and Select2 works, is selected the various items, but at the time of capturing that I am not getting. I used your method that recovers, but when I press the button gives the postback only takes one and the worst when it gives the postback those selected disappear and only the first appears

  • The list as you put it does not work for me, I believe it is another version of component. You have to put the whole code with your references so I can test it here

  • @Fábiomedeirosbitencourt you are not knowing how to do face... This code is functional and this is how it does, now if made a response based on my !?

0

I got my DEV friends with the tip from Virgilio Novic, I just made some modifications Replaces the list with select(HTML)

   <select id="lb" runat="server" class="form-control input-sm select2-multiple" multiple>
    <option value="	1	">	Administração	</option>
<option value="	2	">	Administração Pública	</option>
<option value="	3	">	Agronegócios e Agropecuária	</option>
<option value="	4	">	Ciências Aeronáuticas	</option>
                        </select>

No . Cs put the following code.

 string mensagem="";

    for (int i = 0; i <= lb.Items.Count - 1; i++)
    {
        if (lb.Items[i].Selected)
            mensagem +=  lb.Items[i].Text ;
    }

Now just transfer the variable message to Textbox.

  • If you did the same thing I did, only you didn’t use the component of ToolBox, it is worth remembering that my answer is according to the question and through the ideal principles for development and what you have just done works, but it is not right. The big problem is that you did wrong because you didn’t set up ListBox correctly and therefore the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.