Throwing a popup when the search returns no results

Here's the deal. You have a live web application using ASP.NET WebForms with a C # backend. Everything works fine, but I always try to improve as a newbie to this. Right now, in order to deal with user searches returning without any results, I am using the following and wondering if there is any cleaner way to do this, 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(); } 
+6
source share
5 answers

I agree that you do not use pop-ups, so you can always do something as easy as having a Label object on your page:

 <asp:Label runat="server" id="lblResultMsg" ForeColor="Red" Visible="False" /> 

And then dynamically adjust the text (or add it as a property to the code) and set a label that will be visible during postback if no results are found:

 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 } 

But there are quite a few ways to achieve something like this. As for your question about using .Count from the Enumerable collection - there is nothing stopping you from doing this, since this is absolutely true. The question is, which method do you consider more readable?

0
source

if you enable the jquery ui dialog (http://jqueryui.com/demos/dialog/), you can simply call this to create a nice dialog box:

 $('<div>message</div>').dialog({autoOpen:true,title:'Error'}); 
0
source

Personally, I prefer to create a helper function to insert the appropriate javascript on the page and only pass the parameters of the function, so I don’t have to worry about the messy details every time.

Sort of:

 public static void GrowlMessage(System.Web.UI.Control pageControl, string header = "", string message = "", bool sticky = false, string position = "top-right", string theme = "", bool closer = true, int life = 8) { string _js = "$.jGrowl('" + HttpContext.Current.Server.HtmlEncode(message) + "', { header:'" + header + "', sticky:" + sticky.ToString().ToLower() + ", position: '" + position + "', theme: '" + theme + "', closer: " + closer.ToString().ToLower() + ", life:" + life * 1000 + "});"; ScriptManager.RegisterStartupScript(pageControl, pageControl.GetType(),"Growl",_js, true); } 

The sample I used also requires jQuery and the jGrowl library available. And IMHO the messages are good. They are unobtrusive, the user does not need to press a button to make them leave, and they disappear after a certain amount of time.

But I agree with Mike that if you don’t have any entries, you should simply use the built-in GridView properties (EmptyDataRowStyle and EmptyDataRowText) to display a message saying β€œno data matching your request”. Assuming you use a gridview in general, this is ..

0
source

When it comes to user reviews, Impromptu is my friend. There is a good implementation of ASP.NET Impromptu on the Aaron Goldenthal website: http://www.aarongoldenthal.com/post/2009/11/11/Using-jQuery-Impromptu-With-ASPNET.aspx

0
source

If you decide to warn the user using an alert, then please use the light box effect.

http://www.designyourway.net/blog/resources/30-efficient-jquery-lightbox-plugins/

if you still want to continue the traditional alert, then obviously it’s easy for you to run it to load the page, rather than attach a script to it.

') "....>

Because if you need any changes, you just need to change javascript yourself, and you don't need to build the project again to check it ...

Hope this is helpful for you.

Note. I use my own DLLs to render content, so a change may be required over the encoding because I forgot the traditional asp encodings .. :)

0
source

Source: https://habr.com/ru/post/908730/


All Articles