Solved

The best way to show messages to user

Posted on 2010-08-15
13
955 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A short film showing how OnPage and Connectwise integration works.

911 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

20 Experts available now in Live!

Get 1:1 Help Now