在我选择了具有相关主键的下拉列表项后自动选择检查表项

本文关键字:选择 下拉列表 检查表 | 更新日期: 2023-09-27 18:18:44

我需要一些帮助在ddlstore_selectedindexchange事件下的代码。我试图让员工和客户检查表自动检查他们的外键Id是否等于商店的主键Id。我不知道IF语句里该放些什么。

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        using (StuffContainer context = new StuffContainer())
        {
            ddlstore.DataSource = context.Stores.ToList();
            ddlstore.DataTextField = "Name";
            ddlstore.DataValueField = "Id";
            ddlstore.DataBind();
            chkemp.DataSource = context.Customers.ToList();
            chkemp.DataTextField = "FName";
            chkemp.DataValueField = "Id";
            chkemp.DataBind();
            chkcust.DataSource = context.Employees.ToList();
            chkcust.DataTextField = "FName";
            chkcust.DataValueField = "Id";
        }
    }
}
protected void ddlstore_SelectedIndexChanged(object sender, EventArgs e)
{
    int storeId = int.Parse(ddlstore.SelectedValue);
    using (StuffContainer context = new StuffContainer())
    {
        List<Employee> employees = context.Employees.ToList();
        var employee = context.Employees.Where(x => x.StoreId == storeId);
        foreach(Employee item in employees)
        {
           if()
           {
           }
        }
    }
}

在我选择了具有相关主键的下拉列表项后自动选择检查表项

protected void ddlstore_SelectedIndexChanged(object sender, EventArgs e)
{
    int storeId = int.Parse(ddlstore.SelectedValue);
    using (StuffContainer context = new StuffContainer())
    {
        var employees = context.Employees
           .Where(x => x.StoreId == storeId)
           .Select(x => x.Id).ToList();
        var customers = context.Customers
           .Where(x => x.StoreId == storeId)
           .Select(x => x.Id).ToList();
        foreach(ListItem item in chkemp.Items)
            item.Selected = employees.Contains(int.Parse(item.Value));
        foreach(ListItem item in chkcust.Items)
            item.Selected = customers.Contains(int.Parse(item.Value));
    }
}