Recommendations for good ways to display user messages in asp.net? Recommendations for good ways to display user messages in asp.net? asp.net asp.net

Recommendations for good ways to display user messages in asp.net?


Why not a simple HTML "old-school" extension method? (i.e static method)

namespace Web.Helpers{    public class HtmlHelpers     {         public static string Label(string target)         {              var dynamicText = SomeWhere.GetSomeCoolText();              return String.Format("<label for='{0}'>{1}</label>", target, dynamicText);         }    }}

Then in your HTML:

<%= HtmlHelpers.Label("sometarget") =>

In your main master page, just import the namespace:

<%@ Import Namespace="Web.Helpers" %>

EDIT after question updated

Ok, i see what you're trying to do now. I would recommend doing it all client-side.

That is, in your main master page, create an initially hidden <div> with a unique id:

<div id="message" style="display: none"></div>

Then create a helper method somewhere in your web project, rendering out some script.

public void DisplayMessage(){    string message = SomeWhere.GetSomeCoolText();    StringBuilder script = new StringBuilder();    script.AppendFormat("$('message').show().append("<span>{0}</span>")", message);    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), script.ToString(), true);}

Of course, this is just a guideline. You dont have to use jQuery, you don't have to hardcode the script in the method. You could create a JavaScript function that accepts a string and modifies the HTML of the div, then just call the function.

It all depends on how complicated your "message" is, if you need special server controls, internationalization, etc.

But this is certainly the easiest way to accomplish what you want (IMHO).


Personally, I'd prefer using the HttpContext.Current.Items dictionary with a custom control. Similar to Keith Bluestone's approach, but doesn't require an arbitrarily named control on the page; it makes it very clear what's going on. You could package it into one class as a server control easily enough, but here it is to drop into a standard web project.

To register a message in your code behind:

SiteMessageUtility.Add("Test message");

To display in your page, or master page, or wherever, really:

<%@ Register TagPrefix="custom" TagName="SiteMessage" Src="~/Controls/SiteMessage.ascx" %><custom:SiteMessage runat="server" />

Here's the files you'll need:
~\App_Code\SiteMessageUtility.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;public static class SiteMessageUtility{    public static void Add(string message)    {        string currMessage = HttpContext.Current.Items["message"] as string;        if (currMessage == null)        {            HttpContext.Current.Items["message"] = message;        }        else        {            HttpContext.Current.Items["message"] = currMessage + "<br/>" + message;        }    }}

~\Controls\SiteMessage.aspx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SiteMessage.ascx.cs" Inherits="Controls_SiteMessage" %><asp:Literal runat="server" ID="message" />

~\Controls\SiteMessage.aspx.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Controls_SiteMessage : System.Web.UI.UserControl{    protected void Page_Load(object sender, EventArgs e)    {    }    protected override void OnPreRender(EventArgs e)    {        message.Text = (string)HttpContext.Current.Items["message"];        base.OnPreRender(e);    }}


On the master page add the following controls:the hfMsg hidden field will hold the message that will be displayedand the hfCtrl will hold the name of the html control that will display this message which will be in this example lblMsg.

<label id="lblMsg" style="background-color:Yellow; border:solid 1px; width:200px;height:100px;display:none;"></label>        <asp:HiddenField ID="hfMsg" runat="server" />    <asp:HiddenField ID="hfCtrl" runat="server" />

NOTE:you can add another html control with a different id on an ASCX control and use it to display the message instead of the lblMsg.

and the following script:

<script language="javascript" type="text/javascript">    var msg = $('#<%= hfMsg.ClientID %>').attr("value");    var ctrl = $('#<%= hfCtrl.ClientID %>').attr("value");    if (msg != undefined && msg != "") {        $("#" + ctrl).html(msg);                    $("#" + ctrl).show();    }    else {        $("#" + ctrl).hide();    }</script>

NOTE:the script simply checks to see if the hfMsg control has a message to display or not.

and add the following two methods to the master page:

public void ShowMessage(string control, string message){    this.hfCtrl.Value = control;    this.hfMsg.Value = message;}public void ClearMessage(){    this.hfMsg.Value = string.Empty;}

finally you can call the ShowMessage method from any page like this to display a message on the master page:

Master.ShowMessage("lblMsg","hello world!");

and if you have a user control that holds an html label (e.g lblUserMsg) to display the message you can simply call ShowMessage and pass it the name of the label:

Master.ShowMessage("lblUserMsg","hello world!");