Solved

The best way to show messages to user

Posted on 2010-08-15
13
976 Views
Last Modified: 2012-06-27
Hi!

In my website I'm using a Panel with a Label inside to show confirmation messages to user.
For e.g.
When the user fails the login, I set
Panel.visible = true;
Label.Text = "Login Failed";

Sometimes gets hard to handle the panel to visible/invisible.

It does works but I think that is not the better (or prettier) way to do that.

Any suggestions? Maybe some Ajax/JQuery showing the message just for a few seconds...

Thanks in advance!
0
Comment
Question by:calypsoworld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 18

Expert Comment

by:Gary Davis
ID: 33442176
Just set Label.Text to your error or empty if no error. Then you don't need a panel and don't need to set any visible property.
Gary
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 33442188
You could try something like this in the attached code snippet.

From C# codebehind for example, you would call it by:

string messageText = "Hello there!";

StringBuilder sb = new StringBuilder()
sb.AppendLine("<script type=\"text/javascript\">");
sb.AppendLine("showMessage(\"" + messageText + "\"");");
sb.AppendLine("</script>");

Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", sb.ToString());
function showMessage(text)
{
    var msgBox = document.getElementById("divMessage");
    if (msgBox)
    {
        msgBox.innerHTML = text;
        msgBox.style.display = "";
        setTimeout("hideMessage()", 5000);
    }
}
function hideMessage()
{
   var msgBox = document.getElementById("divMessage");
   msgBox.style.display = "none";
}

Open in new window

0
 
LVL 7

Expert Comment

by:Gewgala
ID: 33442195
Forgot to clarify, in the above, the code snippet is Javascript, and it will display your div box for 5 seconds (5000 milliseconds) and then it will hide it automatically afterward.  The top code example above the snippet is how you would execute the javascript using C#.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Adam Menkes
ID: 33442201
0
 

Author Comment

by:calypsoworld
ID: 33442206
Thank you for reply.

I need a Panel to make the message more attractive. If it's just a label appears, maybe user can't see it.

In a GridView, click on a button that returns me a message, this message appears. Ok.
But if I still using the GridView, this message keeps visible, so I put this code on page load method.

(isPostBack) Label.Text = ""

Ok, it works. But there is nothing easier or ''prettier'' ?


Thank you!
0
 
LVL 19

Assisted Solution

by:Adam Menkes
Adam Menkes earned 166 total points
ID: 33442226
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 33442247
My way could be used to make it pretty, you just have to style your div properly.  The div will appear and then disappear after 5 seconds like you were saying.
0
 

Author Comment

by:calypsoworld
ID: 33442278
> Gewgala

The C# code that you posted, must be placed on page load method?

How should I call the JavaScript method showMessage(text) from C#?

Thank you!
0
 
LVL 7

Assisted Solution

by:Gewgala
Gewgala earned 166 total points
ID: 33442406
You can put it in a method, such as the attached snippet.  Then just call it whenever you want to display the message.  Anywhere in your code, if you have the below snippet in your codebehind file:

// some code...

ShowMessage("Your information was saved successfully!");

// some more code...



protected void ShowMessage(string message)
{
     string messageText = "Hello there!";

     StringBuilder sb = new StringBuilder()
     sb.AppendLine("<script type=\"text/javascript\">");
     sb.AppendLine("showMessage(\"" + message + "\"");");
     sb.AppendLine("</script>");

     Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", sb.ToString());
}

Open in new window

0
 
LVL 7

Expert Comment

by:Gewgala
ID: 33442413
Forgot to erase this line:

string messageText = "Hello there!";

from my previous post in the attached snippet.  I copied and pasted the example from before into the method body.  The messageText variable is no longer necessary since the message being passed in is a parameter, and you're just plugging in the parameter to the javascript function call.
0
 
LVL 14

Accepted Solution

by:
robasta earned 168 total points
ID: 33443674
also, consider using the ajaxcontroltoolkit modalpopup - here is an example for a gridview (the site has many useful examples for the control) http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html
0
 

Author Comment

by:calypsoworld
ID: 33563194
Thank you for replies!

I have not tested all the solutions yet.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question