gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:20:52 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com