我有一个小问题,我正在我的 asp.net 应用程序中创建一个编辑页面,用户可以在其中编辑对象中的属性。我有两个下拉列表(类别和组),其中组的数量取决于所选的类别。我的目标是显示正在编辑的对象的正确类别,然后加载组列表并选择正确的组 - 问题是我的 selectedindexchanged 事件永远不会被触发。
当我在 page_load 中加载我的类别并填充类别时,代码如下所示:
protected void Page_Load(object sender, EventArgs e)
{ string editid= Request["edit"] == null ? null : Request["edit"].ToString();
int id = Convert.ToInt32(editid);
if (link == null)
{
link = BLLink.Load(id, blm);
}
if (!IsPostBack)
{
group = BLGroup.Load(link.GroupId, blm);
category = BLCategory.Load(group.CategoryId, blm);
List<BLCategory> categories = BLCategory.LoadAll();
categoryDropDown.DataSource = categories;
categoryDropDown.DataTextField = "CategoryName";
categoryDropDown.DataValueField = "id";
categoryDropDown.SelectedValue = category.id.ToString(); //this one doesnt cause the event to fire??? Doesnt matter if it is called after the databind() method
categoryDropDown.DataBind();
}
}
我要执行的事件处理程序应该加载所有组并填充下拉列表并选择正确的组:
protected void categoryDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
category = BLCategory.Load(Convert.ToInt32(categoryDropDown.SelectedValue), new TestModelDataContext());
if (category != null)
{
List<BLGroup> groups = BLGroup.LoadAll(category.id);
groupDropDown.DataSource = groups;
groupDropDown.DataTextField = "GroupHeading";
groupDropDown.DataValueField = "GroupId";
groupDropDown.DataBind();
if (group != null)
{
groupDropDown.SelectedValue = group.GroupId.ToString();
}
else
{
groupDropDown.SelectedIndex = 0;
}
}
}
我不知道出了什么问题,这似乎是一件简单的事情,我做错了什么?