我如何建立我在asp.net editform其中两个下拉菜单是相互依存?
-
21-08-2019 - |
题
我有一个小问题,我建立在我的asp.net应用一个编辑的页面,用户可在对象编辑属性。我有两个下拉列表(类别和组),其中基团的量取决于所选择的类别。我的目标是显示正在编辑正确的类别OG的对象,然后加载组的列表中,选择正确的组 - 问题是,我的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();
}
}
Theevent处理机我想执行应加载所有组并填充下拉列表并选择正确的一个:
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;
}
}
}
我不知道发生了什么错误,这似乎是一个简单的事情,我究竟做错了什么?
解决方案
要获得此事件,火灾,页面需要回发时,用户的选择变化。这将导致整个页面刷新(但控制应该保持其状态)。
此SelectedIndexChanged事件是在服务器上运行,并且我认为有关于当在其上所选择的项目的更改提交/回发所述第一下拉的属性。
要获得客户端的行为,你可以做两两件事:点击 1)在包裹一个UpdatePanel该部分并有第二个列表刷新时的第一个的值发生变化结果 2)使用时触发对第一个所选择的值的变化,检索列表并填充所述第二个结果,JavaScript客户端代码加载值 图2a),则可以写一个封装该呼叫到数据源,并返回值作为JSON等服务,或 2B),您可以填写每一个可能的选择所有的值成在由第一个框的值某种程度上id'd隐藏字段(这是不可取的,但可以根据您的数据集的安全性和大小)
选项1可能是保留现有的代码库,仍然可以得到客户端行为的最直接的方式。
不隶属于 StackOverflow