【问题标题】:Dynamic Data asp.net Foreign key relationship in DropdownList, how to customise?动态数据asp.net DropdownList中的外键关系,如何自定义?
【发布时间】:2012-09-18 06:29:01
【问题描述】:

我正在处理Asp.Net Dynamic Data project with linq to sql

我创建了一个数据库,该数据库在使用外键指定的两个表之间具有 one to one relationship

即我有一张桌子Employee 和一张桌子Department

Employee table
empid empname address departmentId 

Department table
departmentId departmentname

现在Student 表采用departmentId 作为foreign key

当我导航到/Insert.aspx 时,它会在下拉列表中显示department_name 的列表。

它会这样做 - 但它会将所有 department_name 显示到下拉列表中

我可以如何以及在哪里编写 linq 查询,以便我可以自定义那些部门名称值

如果假设我只想将 computers 填充为该下拉列表中的部门名称 我怎么能做到这一点?

代码: 在 FieldTemplate 文件夹中ForeignKey_Edit.ascx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (DropDownList1.Items.Count == 0)
        {
            if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
            {
                DropDownList1.Items.Add(new ListItem("[Not Set]", ""));
            }
            PopulateListControl(DropDownList1);
        }

        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(DynamicValidator1);
    }

数据动态正在内部填充下拉列表。

在此声明之前或之后PopulateListControl(DropDownList1);

我应该编写什么代码才能只填充特定的值..?

【问题讨论】:

  • 到目前为止你做了什么?你现在是如何填充你的下拉列表的?

标签: asp.net linq-to-sql asp.net-dynamic-data


【解决方案1】:

而不是PopulateListControl(DropDownList1); 试试这样的

        if (ForeignKeyColumn.ParentTable.Name == "Departments")
        {
            List<Department> departments = // code to grab the correct Departments

            foreach (Department d in departments)
            {
                DropDownList1.Items.Add(new ListItem(d.Name, d.Id.ToString()));
            }
        }

【讨论】:

    猜你喜欢
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多