user3210023
user3210023

Reputation: 164

Can't find selected item in listbox C# ASP.NET

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //int test = Convert.ToInt32(ListBox1.SelectedValue.ToString());            
    //GetById(test);
    foreach (ListItem listitem in ListBox1.Items)
    {
        if (listitem.Selected == true)
        {
            string email = listitem.Text;
            GetByEmail(email);
        }
    }
}

This method should get selected item.I don't why but it show me that property selected is false for all items.

protected void Page_Load(object sender, EventArgs e)
{ 
    ListBox1.Items.Clear();
    SelectAllUsers();
}

public void SelectAllUsers()
{
    SchoolService.SchoolServiceClient client = new SchoolService.SchoolServiceClient();
    SchoolService.User[] userArray = client.SelectAll(0);

    int i = 0;

    while (i < userArray.Length)
    {                                
        ListItem listItem = new ListItem();
        listItem.Text = userArray[i].Email;
        listItem.Value = userArray[i].Id.ToString();                                      
        ListBox1.Items.Add(listItem);                
        i++;
    }            
}

public void GetByEmail(string email)
{
    SchoolService.SchoolServiceClient client = new SchoolService.SchoolServiceClient();
    SchoolClient.SchoolService.User user = client.GetUserByEmail(email);

    if ((user.FirstName != null) || (user.LastName != null) || (user.Address != null) ||
        (user.City != null) || (user.Email != null) || (user.Password != null))
    {
        txtId.Text = user.Id.ToString();
        txtFirstName.Text = user.FirstName.ToString();
        txtLastName.Text = user.LastName.ToString();
        txtAddress.Text = user.Address.ToString();
        txtCity.Text = user.City.ToString();
        txtDateOfBirth.Text = user.DateOfBirth.ToShortDateString();
        txtEmail.Text = user.Email.ToString();
        txtPassword.Text = user.Password.ToString();
        txtIsAdmin.Text = user.isAdmin.ToString();
    }
    else
    {
        MsgBox("None user was found", this.Page, this);
    }
}
public void SelectAllUsers()
{
    SchoolService.SchoolServiceClient client = new SchoolService.SchoolServiceClient();
    SchoolService.User[] userArray = client.SelectAll(0);

    int i = 0;

    while (i < userArray.Length)
    {
        //if (userArray[i] != null)
        //{
            ListItem listItem = new ListItem();
            listItem.Text = userArray[i].Email;
            listItem.Value = userArray[i].Id.ToString();                                      
            ListBox1.Items.Add(listItem);
        //}
        i++;
    }            
}

Also sent methods which fill listBox with items (SelectAllUsers), and method which should take selected item from database (GetByEmail)

Upvotes: 1

Views: 912

Answers (1)

Icarus
Icarus

Reputation: 63970

It doesn't find anything selected because on Page_Load you are rebinding the data again every time. You need to check if IsPostBack

protected void Page_Load(object sender, EventArgs e)
{ 
    if(!IsPostBack) 
    {
      ListBox1.Items.Clear();
      SelectAllUsers();
    }
}

Upvotes: 5

Related Questions