当搜索没有结果时抛出弹出窗口

All*_*nko 6 javascript c# asp.net webforms

这是交易.使用带有C#后端的ASP.NET WebForms,有一个功能正常的Web应用程序.事情很好,但我总是希望改进,作为这个东西的初学者.现在,为了处理用户的搜索没有结果,我利用以下内容,并想知道是否有更清洁的方法来做,以备将来参考:

DataClass data = new DataClass();
var searchresults = data.GetData(searchBox.Text);
int datanumber = searchresults.Count();
if (datanumber == 0)
{
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "javascript:alert('There were no records found to match your search');", true);
}
else
{
    DropDownList1.Visible = true;
    DropDownList1.Items.Clear();
    DropDownList1.DataSource = searchresults;
    DropDownList1.DataBind();
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*ann 0

我同意不使用弹出窗口,因此您始终可以做一些简单的事情,例如在页面上添加一个 Label 对象:

<asp:Label runat="server" id="lblResultMsg" ForeColor="Red" Visible="False" />
Run Code Online (Sandbox Code Playgroud)

然后动态设置文本(或将其作为属性添加到代码中),并将标签设置为在未找到结果时在回发时可见:

if (datanumber == 0)
{
    lblResultMsg.Text = "There were no records found to match your search.";
    lblResultMsg.Visible = true;
}
else
{
    lblResultMsg.Text = "";
    lblResultMsg.Visible = false;

    // do your data binding
}
Run Code Online (Sandbox Code Playgroud)

但是有很多方法可以实现这样的目标。关于您关于使用 Enumerable 集合中的 .Count 的问题 - 没有什么可以阻止您这样做,因为它是完全有效的。问题是您认为哪种方法更具可读性?