gpt4 book ai didi

c# - 具有多个 DropDownList 的 UpdatePanel

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:42 25 4
gpt4 key购买 nike

编辑:是 DropDownList.Items.Clear(); 在我列出 db 中的一些内容的方法中导致了问题。但是,我不明白为什么这会导致问题。如果我能给出解释,我将不胜感激,因为我确实需要使用 Items.Clear();不知何故

我有 3 个 DropDownList,我根据在这 3 个 DropDownList 之一中选择的值更新特定标签。每个 DropDownList 都工作正常,直到我选择下一个,而之前的其他人不会通过 UpdatePanel 触发。

例如第一个有效,但在我使用第二个后停止工作(标签不会更新)。第二个有效,但一旦使用了第三个 DropDownList,第二个也无法正常工作,只有第三个 DropDownList 可以正常工作。

如果我立即使用第三个,第一个和第二个 DropDownList 将无法通过 UpdatePanel 工作。

换句话说,一旦最新的 DropDownList 被使用,它之前的每个 DropDownList 都不会通过 UpdatePanel 触发。

aspx:

<asp:DropDownList ID="dpl1" runat="server" OnSelectedIndexChanged="dpl1_OnSelectedIndexChanged" AutoPostBack="True" />
<asp:DropDownList ID="dpl2" runat="server" OnSelectedIndexChanged="dpl2_OnSelectedIndexChanged" AutoPostBack="True" />
<asp:DropDownList ID="dpl3" runat="server" OnSelectedIndexChanged="dpl3_OnSelectedIndexChanged" AutoPostBack="True" />

<asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Label ID="lblDPB" runat="server"/>
</ContentTemplate>

<Triggers>
<asp:AsyncPostBackTrigger ControlID="dpl1" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="dpl2" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="dpl3" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>

代码隐藏:

 protected void dpl1_OnSelectedIndexChanged(object sender, EventArgs e) {

lblDPB.Text = "#1: ";
}

protected void dpl2_OnSelectedIndexChanged(object sender, EventArgs e) {
lblDPB.Text = "#2: ";
}

protected void dpl3_OnSelectedIndexChanged(object sender, EventArgs e)
{
lblDPB.Text = "#3: ";
}

如何同时制作所有这 3 个作品?

谢谢

最佳答案

在每次回发时清除和填充 DropDownList 会弄乱列表中的选择。调用 Items.Clear() 清空 SelectedValue 并将 SelectedIndex 设置为 -1。填满列表后,第一项被选中。所有这些处理都会导致 SelectedIndexChanged 事件在意外时刻触发。

避免此问题的一种方法是在调用 Items.Clear() 之前保存选定的值,并在重新填充列表后将它们重新设置:

protected void Page_Load(object sender, EventArgs e)
{
...

string val1 = dpl1.SelectedValue;
string val2 = dpl2.SelectedValue;
string val3 = dpl3.SelectedValue;

dpl1.Items.Clear();
dpl2.Items.Clear();
dpl3.Items.Clear();

// Fill the lists here

SafeSelectValue(dpl1, val1);
SafeSelectValue(dpl2, val2);
SafeSelectValue(dpl3, val3);
}

private void SafeSelectValue(ListControl lst, string value)
{
// Makes sure that the value exists before selecting it
if (lst.Items.FindByValue(value) != null)
{
lst.SelectedValue = value;
}
}

顺便说一下,为了查看列表项的变化,我需要将三个 DropDownLists 放在一个带有 UpdateMode="Always" 的 UpdatePanel 中:

<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:DropDownList ID="dpl1" runat="server" OnSelectedIndexChanged="dpl1_OnSelectedIndexChanged" AutoPostBack="True" />
<asp:DropDownList ID="dpl2" runat="server" OnSelectedIndexChanged="dpl2_OnSelectedIndexChanged" AutoPostBack="True" />
<asp:DropDownList ID="dpl3" runat="server" OnSelectedIndexChanged="dpl3_OnSelectedIndexChanged" AutoPostBack="True" />
</ContentTemplate>
</asp:UpdatePanel>

关于c# - 具有多个 DropDownList 的 UpdatePanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663012/

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