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
Solved

The best way to show messages to user

Posted on 2010-08-15
13
971 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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