【问题标题】:Why is my DropDownList in asp.net not refreshing?为什么我在 asp.net 中的 DropDownList 不刷新?
【发布时间】:2014-05-24 19:43:39
【问题描述】:

我的 asp.net 网页上有一个简单的 DropDownList:

看起来像这样:

       <asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">

            <ContentTemplate>
                <asp:DropDownList ID="FriendList" runat="server" 
                     EnableViewState="False" Height="30px" Width="10%"  
                     OnTextChanged="FriendListSwitch" AutoPostBack="True">
                </asp:DropDownList>
            </ContentTemplate>

            <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnSend" />
            </Triggers>
            <--!The button is a stub, I actually need to load it from the codebehind-->

       </asp:UpdatePanel>

然后我有这样的代码隐藏:

void OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
{
    FriendList.Items.Add(new ListItem(String.Format("{0}", item.Jid.User), ""));
    FriendList.DataBind();
}

在调试时,我可以看到“FriendList”正在被填充,并且“btnSend”正在被点击。

然而我的 DropDownList 并没有自动填充,它只是保持空白,为什么不刷新?

编辑:我宁愿删除按钮并将其替换为

 void OnRosterEnd(object sender)
     {
         UpdatePanel5.Update();
     }

但后来我不断得到

The Update method can only be called on UpdatePanel with ID 'X' before Render.

已解决,DataBind() 只在我的 Page_Load 中工作。

【问题讨论】:

    标签: c# asp.net drop-down-menu webforms refresh


    【解决方案1】:

    尝试使用 OnSelectedIndexChanged 事件而不是 OnTextChanged

    【讨论】:

    • 其实盒子的那部分是工作的,我用预设值测试了它
    【解决方案2】:

    “btnSend”按钮应该在更新面板内

    【讨论】:

    • 我有多个更新面板使用面板外的触发器按预期工作,我尝试将它放在面板内,但没有运气。
    【解决方案3】:

    如果您需要按钮来更新下拉菜单,那么它需要在更新面板中

    如果您需要下拉列表作为触发器,那么 asyncpostback 触发器需要具有下拉列表的控件 ID

    【讨论】:

    • 按钮(或者更确切地说来自代码隐藏的东西)应该更新它。只是把它放在两者之间是行不通的。
    • 为什么不能把按钮放在更新面板里?如果它最初需要不可见,只需将 Visible 属性设置为 false。如果要从按钮更新下拉菜单中的数据,则按钮需要在更新面板中
    • 我确实将按钮放在更新面板中,不幸的是它没有任何改变,问题仍然存在......
    • 是的,我添加了一个脚本管理器,它正在其他几个更新面板上工作。
    • 您说您已将按钮添加到更新面板,但我在您的问题中没有看到。您能否更新您的问题以反映更新面板中的按钮,以便我们正确排除故障
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多