gpt4 book ai didi

c# - 是否可以在 SelectedIndexChanged 事件触发回发时重新绑定(bind) DropDownList?

转载 作者:行者123 更新时间:2023-11-30 19:32:45 25 4
gpt4 key购买 nike

假设我有一个产品类别的 DropDownList 和一个基于 DropDownList 中的类别选择的产品 ListView 。当用户访问该页面时,可能会出现一致性问题,因为在用户浏览时属于新类别的新产品可能会添加到库存中。

当用户选择不同的类别来查看(SelectedIndexChanged 事件)并导致回发时,我希望 DropDownList 更新类别列表以包括同时添加的新类别,同时仍然能够进行所选索引的更改。 DropDownList 似乎不支持在回发时进行此更新。那么我该如何处理这个并发问题呢?

最佳答案

无论是否检查 Page_Init 事件中的回发下拉列表,您都需要绑定(bind)和重新绑定(bind)。它不会导致下拉列表的选定索引发生任何变化,您可以继续正常工作,在加载 View 状态之前调用页面初始化方法。意味着您的下拉索引将在此方法之后被选中,您可以在 dropdown_selectedIndexChanged 方法中访问它。

有关 ASP.Net page life cycle 的更多信息

编辑 1:查看示例代码

protected void Page_Init(object sender, EventArgs e)
{
// here you bind your dropdown
// don't check IsPostBack
DropdownList1.DataSource = db.GetCategories();
DropdownList1.DataTextField = "TextField";
DropdownList1.DataValueField = "ValueField";
DropdownList1.DataBind();
}

ASP.Net 在 Page_InitPage_Load 事件之前加载控件 View 状态,因此 DropDownList1 的 selectedIndex 不会受到影响,并且您得到了预期的结果。

关于c# - 是否可以在 SelectedIndexChanged 事件触发回发时重新绑定(bind) DropDownList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4887572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com