Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从数据库选项卡';这是一排吗?_C#_Database_Drop Down Menu_Listitem - Fatal编程技术网

C# 如何从数据库选项卡';这是一排吗?

C# 如何从数据库选项卡';这是一排吗?,c#,database,drop-down-menu,listitem,C#,Database,Drop Down Menu,Listitem,我使用实体框架连接到数据库并从表中检索一些信息。我需要在下拉列表中显示此信息。我需要将每个列表项的值设置为数据库中设置的Id public void EducationDropDownListViewer() { EducationDropdown.Items.Add(new ListItem { Text = "--select--", Value = "0" }); List<Education> educations = ModelLists.GetEducat

我使用实体框架连接到数据库并从表中检索一些信息。我需要在下拉列表中显示此信息。我需要将每个列表项的值设置为数据库中设置的Id

public void EducationDropDownListViewer()
{
    EducationDropdown.Items.Add(new ListItem { Text = "--select--", Value = "0" });
    List<Education> educations = ModelLists.GetEducationList();
    for (int i = 0; i < educations.Count; i++)
    {
        ListItem educationListItem = new ListItem();
        Education education = educations[i];
        educationListItem.Text = education.EducationName;
        educationListItem.Value = education.Id.ToString(CultureInfo.InvariantCulture);
        EducationDropdown.Items.Add(educationListItem);
    }
}

SelectedIndex
属性返回整个下拉列表中当前选择项的累进索引。
您正在查找返回
SelectedItem的
值的
SelectedValue
属性

protected void EducationDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    // If you accept also the item at index zero (the prompt to select) then change
    // the test below to >=
    if(EducationDropDown.SelectedIndex > 0)
        ViewState["educationid"] = Convert.ToInt32(EducationDropdown.SelectedValue);
}
protected void EducationDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    // If you accept also the item at index zero (the prompt to select) then change
    // the test below to >=
    if(EducationDropDown.SelectedIndex > 0)
        ViewState["educationid"] = Convert.ToInt32(EducationDropdown.SelectedValue);
}