0

我有一个小问题,我正在我的 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;
            }
        }
    }

我不知道出了什么问题,这似乎是一件简单的事情,我做错了什么?

4

1 回答 1

0

要触发此事件,页面需要在用户选择更改时回发。这将导致整个页面刷新(但控件应保持其状态)。

此 SelectedIndexChanged 事件正在服务器上运行,我认为当所选项目发生更改时,第一个下拉列表中有一个关于提交/回发的属性。

要获得客户端行为,您可以做两件事:
1)将该部分包装在 UpdatePanel 中,并在第一个值更改时刷新第二个列表
2)使用 JavaScript 客户端代码加载值,该代码在选定值时触发更改第一个,检索列表并填充第二个
2a)您可以编写一个服务来包装对数据源的调用并将值作为 JSON 等返回,或者 2b)您可以填充所有值每个可能的选择到隐藏字段中,这些字段以某种方式由第一个框中的值标识(这是不可取的,但可能取决于数据集的安全性和大小)。

选项 1 可能是保留现有代码库并仍然获得客户端行为的最直接方法。

于 2009-06-30T21:57:50.903 回答