c# - 无法在列表框中找到所选项目 C# ASP.NET

标签 c# asp.net listbox

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);
        }
    }
}

此方法应该获取选定的项目。我不知道为什么,但它显示属性 selected 对于所有项目都是 false。

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++;
    }            
}

还发送了用项目填充列表框的方法(SelectAllUsers),以及从数据库中获取所选项目的方法(GetByEmail)

最佳答案

它没有找到任何选定的内容,因为在 Page_Load 上您每次都会重新绑定(bind)数据。您需要检查是否IsPostBack

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

关于c# - 无法在列表框中找到所选项目 C# ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759861/

相关文章:

c# - 由于其保护级别,属性(property)无法进入

Javascript焦点删除文本突出显示

c# - 是否有可能使这个 Flex/Flash 应用程序安全?

linux - gtk-listbox - 内容显示在水平线的中间,列表框不适合窗口大小

C++ Nana 如何从全局列表框 * 指针访问列表框内索引 0 上的按钮?

c# - BinaryWriter Endian 问题

c# - 为什么我可以使用集合初始值设定项和来自另一个类的私有(private)集合访问?

c# - WPF/C# - ListBox 示例什么是 ItemsSource

c# - 如何在列表中存储值类型的引用?

asp.net - 安装 MvcScaffolding 包时出现 NuGet 执行策略错误