locked
how can I get the first index of a listbox ? RRS feed

  • Question

  • User-683124725 posted
    Hi all :)

    how can I get the first index or specified index of a list box ?

    for example:

    I want to put the value of a the first or second index as a text in a textbox

    how can I do that ?

    thank you all :)
    Wednesday, September 21, 2011 8:51 AM

Answers

  • User-1770227574 posted

    Hi Msa,

    Are these 5 items dynamically added?

    That we should get the value after databind.

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2011 5:00 AM

All replies

  • User1654722353 posted

    Hello,

    You can get the value from listbox as below:

    ListBox1.Items[<index>].Text Or ListBox1.Items[<index>].Value
    Wednesday, September 21, 2011 8:58 AM
  • User191633014 posted

    try:

    TextBox1.Text = ListBox1.Items[0].Text;   
    Wednesday, September 21, 2011 9:00 AM
  • User-1419041918 posted

    Find the below code for reference.in that 1 is item index of listbox 

    TextBox1.Text = ListBox1.Items[1].Value.ToString();

    Markas answered if this helps you....

    Thanks.

    Wednesday, September 21, 2011 9:00 AM
  • User-683124725 posted
    thank you 

    Smile | :)

     
     
    but when I try to put this line at page load method
     
    it gives me this message
     
     Collapse | Copy Code
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
     
    and I have about 5 items in my table bounded to this listbox
    Wednesday, September 21, 2011 9:13 AM
  • User1654722353 posted

    Hello,

    In which event did you bind the listbox?

    Or are you statically adding items in listbox?

    Wednesday, September 21, 2011 9:18 AM
  • User-1770227574 posted

    Hi Msa,

    Are these 5 items dynamically added?

    That we should get the value after databind.

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2011 5:00 AM
  • User-448512826 posted

    Hi,

    check Listbox is load or not before get the index?

    Thanks....

    Friday, September 23, 2011 5:40 AM