【问题标题】:Not using asp listbox well没有很好地使用asp列表框
【发布时间】:2012-02-15 20:19:47
【问题描述】:

我想要做的只是单击一个按钮,文本框中的文本会自动添加为列表框中的项目。这不应该直截了当吗?在调试时,添加了项目,我可以通过查看 ListBox1.Items[0] 看到文本,但网页中没有显示任何内容。我在控制台应用程序中遇到了同样的问题,但我没有解决!有人可以指导我做错什么吗?

protected void Button1_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Add(new ListItem(TextBox1.Text));
    }

非常感谢

编辑:

在过去的一个项目中,我使用了 DataSource 属性,效果很好。我还没有设法使用添加项目!可能有某种刷新或更新?

页面代码:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="295px"></asp:ListBox>

<asp:UpdatePanel ID="updatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>

【问题讨论】:

  • 它是空的!但这是我的 aspx 页面。 ListBox>

标签: c# asp.net listbox scriptmanager


【解决方案1】:

看起来您的列表框位于更新面板之外。在更新面板中弹出它:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="AddItem" />
</ContentTemplate>
</asp:UpdatePanel>

【讨论】:

  • 谢谢你!像魅力一样工作!现在至少我知道我正在编写的代码是好的,我必须找到与我的控制台应用程序相同的问题!再次感谢!
【解决方案2】:

您必须将 ListBox 移动到 UpdatePanel 中,否则它不会被更新。

原因是 ASP.NET 将 UpdatePanel 的整个 HTML 发送回客户端。由于 ListBox 不是 UpdatePanel 的一部分,因此不会更新。

【讨论】:

    猜你喜欢
    • 2011-08-05
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2018-10-05
    • 2012-06-11
    • 2020-09-22
    • 2020-01-15
    相关资源
    最近更新 更多