C# 如何从数据库选项卡';这是一排吗?
我使用实体框架连接到数据库并从表中检索一些信息。我需要在下拉列表中显示此信息。我需要将每个列表项的值设置为数据库中设置的IdC# 如何从数据库选项卡';这是一排吗?,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
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);
}