1

I have a one list box and one text box, i want to add list box value to text box on Button Click , but its not working correctly, can you please help me ,i can add the item for text box but cant add more item for text box (MultiSelect)Thanks in advance

TextBox

<div class="col-sm-3">
    <asp:TextBox ID="txtDesignName" runat="server" CssClass="form-control" Width="250px" ></asp:TextBox>
 </div>     

listBox

<div class="col-sm-3">
    <asp:ListBox ID="lstValue" runat="server" CssClass="content" Rows="5" ValidationGroup="save"
               Width="250" OnSelectedIndexChanged="lstValues_SelectedIndexChanged"></asp:ListBox>
</div>

Add Button

<asp:Button ID="btnAdd" runat="server" CssClass="button" Text="Add" ValidationGroup="add"
         Width="70" OnClick="btnAdd_Click" />

CodeBehind

protected void btnAdd_Click(object sender, EventArgs e)
{
    //if(txtDesignName.Text.Equals(string.Empty))
    //{
    //    return; 
    //}
    //else
    //{
        lstValue.Items.Add(txtDesignName.Text);
    //}
}

lstValues_SelectedIndexChanged

protected void lstValues_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtQlt.Text = lstValue.SelectedItem.ToString();  
        }
4
  • are you trying to add items to the lists box or the textbox ? lstValue.Items.Add(txtDesignName.Text); will add items to the listbox.Mybe you got it mixed up Commented Mar 7, 2016 at 3:27
  • i want to add List box items to text box, Commented Mar 7, 2016 at 3:33
  • Is it multi-select list box? Commented Mar 7, 2016 at 3:48
  • @codeninja.sj Sir, its a Multi-Select list box Commented Mar 7, 2016 at 3:49

1 Answer 1

3

The question, you have asked and the code you have shown are both representing different purposes. So, I provided the two options; choose whatever you want.

To display all selected list box-items to your text-box

protected void btnDisplay_Click(object sender, EventArgs e) //create display button to do this process
{
  string selectedItems = string.Empty;
  string separator = ",";
  foreach (int i in lstValue.GetSelectedIndices())
  {
   selectedItems += lstValue.Items[i] + separator;
  }
  txtDesignName.Text = selectedItems.Trim(',');
}

To add text-box value to list-box items

protected void btnAdd_Click(object sender, EventArgs e)
{
  if (txtDesignName.Text.Trim() != string.Empty)
  {
    lstValue.Items.Add(new ListItem(txtDesignName.Text));
    txtDesignName.Text = string.Empty; //reset the text-box value
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Code is working, but cant add multi select value, im add the value its work but im add another value its working, always replace the add 1st value for it, example List Box Value (AA),(BB) , Firstly add the AA , secondly add BB , its not see AA , i want to Display AA BB
I guess, You may reset the list box items in the page_load event. Can you show your full code?
Sir , I Do it, Its Working now, Thanks For the help ) its working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.