这是交易。使用带有 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();
}
最佳答案
我同意不使用弹出窗口,所以你总是可以做一些简单的事情,比如在你的页面上有一个标签对象:
<asp:Label runat="server" id="lblResultMsg" ForeColor="Red" Visible="False" />
然后动态设置文本(或将其作为属性添加到代码中)并将标签设置为在未找到结果时在回发时可见:
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
}
但是有很多方法可以实现这样的目标。关于您关于使用 Enumerable 集合中的 .Count 的问题 - 没有什么可以阻止您这样做,因为它是完全有效的。问题是您认为哪种方法更具可读性?
关于c# - 当搜索没有结果时抛出一个弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9333298/