使用列表框处理查询中的空数据

本文关键字:数据 查询 处理 列表 | 更新日期: 2023-09-27 18:37:19

我目前正在从我的第一个列表框中选择一个值,并根据该值填充我的第二个列表框中的值。但是,在某些情况下,第一个列表框中的值可能不会返回任何内容。我想知道在所选值不返回任何内容的情况下,我将如何在屏幕上显示文本,例如"未返回任何数据"。这可能吗?我正在为两个列表框使用 sqldatasources。

<asp:ListBox ID="SectionItemListBox" DataSourceID="SectionItemSource" runat="server" AutoPostBack="True" DataTextField="SectionItem" DataValueField="SectionItemID" AppendDataBoundItems="False" EnableViewState="True" OnSelectedIndexChanged="SectionItemListBoxSelectedIndexChanged">
</asp:ListBox>

<div style="width:800px; height:auto; overflow:auto">
    <asp:ListBox ID="SectionItemInstructionListBox" DataSourceID="SectionItemInstructionSource" runat="server" DataTextField="Instruction" Visible="True" />
</div>

使用列表框处理查询中的空数据

我会选择类似的东西...

<div style="width:800px; height:auto; overflow:auto">
    <asp:ListBox ID="SectionItemInstructionListBox" DataSourceID="SectionItemInstructionSource" runat="server" DataTextField="Instruction" Visible="True" OnDataBound="SectionItemInstructionListBox_OnDataBound" />
    <asp:Panel ID="NoDataReturnedPanel" Visible="false">
        No Data Returned
    </asp:Panel>
</div>
protected void SectionItemInstructionListBox_OnDataBound(object sender, EventArgs e)
{
    NoDataReturnedPanel.Visible = SectionItemInstructionListBox.Items.Count == 0;
    SectionItemInstructionListBox.Visible = SectionItemInstructionListBox.Items.Count != 0;
}