Solved

The best way to show messages to user

Posted on 2010-08-15
13
948 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
  • 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
 
LVL 19

Expert Comment

by:amenkes
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:amenkes
amenkes earned 166 total points
ID: 33442226
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now