在我选择了带有相关主键的下拉列表项后自动选择核对表项

时间:2015-10-20 19:31:28

标签: c# asp.net drop-down-menu

我需要帮助ddlstore_selectedindexchange事件下的代码。我正在尝试让员工和客户清单自动检查他们的外键ID是否等于商店主键ID。我无法弄清楚IF STATEMENT中要放什么。

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()
           {

           }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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