当搜索没有结果时抛出弹出窗口
事情是这样的。拥有一个使用 ASP.NET WebForms 和 C# 后端运行的 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();
}
Here's the deal. Have a functioning web app using ASP.NET WebForms with a C# backend. The thing works fine, but I'm always looking to improve, as a beginner at this stuff. Right now, to deal with a user's search coming back with no results, I utilize the following, and was wondering if there was any cleaner way to do it, for future reference:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我同意不使用弹出窗口,因此您始终可以做一些简单的事情,例如在页面上放置一个 Label 对象:
然后动态设置文本(或将其作为属性添加到代码中)并将标签设置为在如果没有找到结果则回发:
但是有很多方法可以实现这样的目标。关于您关于使用 Enumerable 集合中的 .Count 的问题 - 没有什么可以阻止您这样做,因为它是完全有效的。问题是您认为哪种方法更具可读性?
I agree with the not using popups, so you could always do something as simple as having a Label object on your page:
And then set the text dynamically (or add it as a property to the code) and set the label to be visible on postback if no results are found:
But there are quite a vast number of ways you could achieve something like this. Regarding your question about using the .Count from the Enumerable collection - there's nothing stopping you doing this as it's perfectly valid. The question is which method do you find more readable?
如果您包含 jquery ui 对话框(http://jqueryui.com/demos/dialog/),您可以简单地调用它来创建一个漂亮的对话框:
if you include the jquery ui dialog (http://jqueryui.com/demos/dialog/), you can simply call this to create a nice dialog box:
就我个人而言,我更喜欢创建一个辅助函数来将相关的 javascript 插入到页面中,并且只将参数传递给函数,这样我就不需要每次都担心混乱的细节。
类似于:
我使用的示例还需要 jQuery 和可用的 jGrowl 库 此处。恕我直言,这些消息很漂亮。它们不引人注目,用户不需要单击按钮即可使它们消失,并且它们会在您指定的时间后消失。
但我同意 Mike 的观点,如果您没有任何记录,则应该使用 GridView 的内置属性(EmptyDataRowStyle 和 EmptyDataRowText)来显示“没有与您的查询匹配的数据”样式消息。假设您正在使用 GridView,那就是..
Personally I prefer to create a helper function for inserting the relevant javascript into the page, and only pass parameters to the function so that I don't need to worry about the messy details every time.
Something like :
The sample I have used also requires jQuery and the jGrowl library available here. And IMHO the messages are pretty. They are unobtrusive, the user does not need to click a button to make them go away, and they fade away after your specified amount of time.
But I agree with Mike, that if you don't have any records, you should just use the built in properties of a GridView (EmptyDataRowStyle and EmptyDataRowText) to display a 'no data matching your query' style message. Assuming that you're using a GridView at all, that is..
当谈到用户反馈时,Impromptu 是我的朋友。 Aaron Goldenthal 的网站上有一个很好的 Impromptu ASP.NET 实现:http://www.aarongoldenthal.com/post/2009/11/11/Using-jQuery-Impromptu-With-ASPNET.aspx
When it comes to user feedback, Impromptu is my friend. There is a nice ASP.NET implementation of Impromptu on Aaron Goldenthal's website: http://www.aarongoldenthal.com/post/2009/11/11/Using-jQuery-Impromptu-With-ASPNET.aspx
如果您决定通过警报提醒用户,请继续使用灯箱效果。
http://www.designyourway.net/blog/resources/30-efficient-jquery-lightbox-plugins/
如果您仍然想继续使用传统的警报,那么显然它很容易你把它点燃在页面加载时而不是附加脚本..
')" ....>
因为如果您需要任何更改,那么您只需要单独更改 javascript,并且不需要再次构建项目来测试它...
希望它对你很有用..
注意:我使用自己的 DLL 来呈现内容,因此上面的编码可能需要更改,因为我确实忘记了传统的 asp 编码.. :)
If you have decided to alert user via alert then please go ahead with light box effect..
http://www.designyourway.net/blog/resources/30-efficient-jquery-lightbox-plugins/
if you are still would like to go ahead with traditional alert then obviously its easy for you to fire it up on page load rather than attaching script to it..
')" ....>
Because if you require any change then you just need to alter the javascript alone and you dont need to build project again to test it...
Hope its useful for you..
Note: I'm using my own DLLs to render content so above coding may requires alteration because i did forget traditional asp codings.. :)