none
Unable to get SelectedIndex value after Items.Clear in ListBox RRS feed

  • Question

  • Every 5 seconds bind new data to listbox, before binding i will clear the existing data in the Listbox using Items.Clear().After clearing the items im not able to get SelectedItem text from the listbox and And also I need to clear the data in list box before binding the new data and i want to get the selected item of the list box

    1) Load Data not on Before PostBack, Load Data every PostBack.....
       protected void Page_Load(object sender, EventArgs e)
        {
                    if (!IsPostBack)
                    {
                       //Not Loading Data on !IsPostBack condition, Every 5 seconds it will get new data.
                    }
                        LoadAciveEmployees();
        }

        2) Bind Values To ListBox

        protected void LoadAciveEmployees()
        {
                DataTable dataContainer = (getting Data from sqlserver)  

                lbxActiveEmp.Items.Clear();
                lbxActiveEmp.DataTextField = "Name";
                lbxActiveEmp.DataValueField = "EmpID";

                foreach (DataRow dr in dataContainer.Rows)
                {
                    string empID = dr["EmpID"].ToString();
                    string name = dr["Name"].ToString();
                    lbxActiveEmp.Items.Add(new ListItem(name + " - " + empID, empID));
                }

        }

    3) Get Selected Value from listtbox,

        protected void getValue()
        {
            if (lbxActiveEmp.SelectedIndex != -1) //Based on this condition get selected value
            {
                        selectedUser = lbxActiveEmp.SelectedItem.Text.ToString(); ***
            }

        }

    *** -- how get SelectedItem (text or value), Is it possible to get selected item text after items are cleared?
    Please help me out from this issue.

    Monday, November 11, 2013 10:09 AM

Answers

  • (Sorry for not adding it to the default answer. Cookies got cleared so I can't edit that message anymore...)

    Anyway, you could just simply do:

            int SelectedVar = 0;
            private void lbxActiveEmp_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lbxActiveEmp.SelectedIndex > -1)
                {
                    SelectedVar = lbxActiveEmp.SelectedIndex;
                }
            }
    selectedUser = lbxActiveEmp.Items[SelectedVar].ToString()

    // Johan







    Monday, November 11, 2013 12:50 PM

All replies

  • ASP.NET questions should be posted in the ASP.NET forums: http://forums.asp.net
    Monday, November 11, 2013 10:11 AM
    Moderator
  • Selected index will get set to -1 as part of the clear which indicates no row is selected.  You could is testing for -1 so no rows are returned.  If you want a default row to be return like the first or last you have to set selected index to a value other than -1 after you fill the listbox.

    jdweng

    Monday, November 11, 2013 10:14 AM
  • can you explain with code...
    Monday, November 11, 2013 10:18 AM
  • foreach (DataRow dr in dataContainer.Rows)
                {
                    string empID = dr["EmpID"].ToString();
                    string name = dr["Name"].ToString();
                    lbxActiveEmp.Items.Add(new ListItem(name + " - " + empID, empID));
                }
                lbxActiveEmp.SelectedIndex = 0;


    jdweng

    Monday, November 11, 2013 11:15 AM
  • You must getSelectedData and after that clear list

    Monday, November 11, 2013 11:29 AM
  • (Sorry for not adding it to the default answer. Cookies got cleared so I can't edit that message anymore...)

    Anyway, you could just simply do:

            int SelectedVar = 0;
            private void lbxActiveEmp_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lbxActiveEmp.SelectedIndex > -1)
                {
                    SelectedVar = lbxActiveEmp.SelectedIndex;
                }
            }
    selectedUser = lbxActiveEmp.Items[SelectedVar].ToString()

    // Johan







    Monday, November 11, 2013 12:50 PM