Monday, August 10, 2009

Use Dynamic stylesheet class for messages in Asp.Net

Hi,

In the web application, some times we need to use a message style for
only one Label, like, suppose if our data has been added successfully, we display
message like 'Records has been added successfully'. So, depend on
system's different situation, we have to display label message with style color
combination.

So, Here you can find the solution. By it, you can display your message
with sytle as per your system's situation.

Just copy and paste the style script and copy C# method in your page.

You can find it here...

<style type="text/css">

.MessageSuccess
{
background: #EFF4EA url(images/icn_successful.gif) center no-repeat;
background-position: 15px 5px; /* x-pos y-pos */
font-weight:bold;
text-align: left;
padding: 5px 20px 5px 45px;
border-top: 1px solid #1E8B18;
border-bottom: 1px solid #1E8B18;
color:#555555;

}
.MessageInfo
{
background: #FFFFD2 url(images/icn_info.gif) center no-repeat;
background-position: 15px 5px; /* x-pos y-pos */
text-align: left;
font-weight:bold;
padding: 5px 20px 5px 45px;
border-top: 1px solid #CACA00;
border-bottom: 1px solid #CACA00;
color:#555555;

}
.MessageError
{
background: #FFEAEA url(images/icn_error.gif) center no-repeat;
background-position: 15px 5px; /* x-pos y-pos */
text-align: left;
font-weight:bold;
padding: 5px 20px 5px 45px;
border-top: 1px solid #FF6F6F;
border-bottom: 1px solid #FF6F6F;
color:#555555;

}
</style>

(Download these images for stylesheet)




<form id="form1" runat="server">
<asp:Label ID="lblMessage" runat="server" />
</form>

protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "Records has been Added Successfully.";
this.SetStyle(lblMessage, MessageType.Info);
}

private void SetStyle(Label objLabel, MessageType msgType)
{
if (string.IsNullOrEmpty(objLabel.Text.Trim()))
objLabel.CssClass = "";
else
{
switch (msgType)
{
case MessageType.Error:
objLabel.CssClass = "MessageError";
break;
case MessageType.Info:
objLabel.CssClass = "MessageInfo";
break;
case MessageType.Success:
objLabel.CssClass = "MessageSuccess";
break;
}
}
}

// Define enum in the outer side of your page class.
public enum MessageType
{
Success,
Info,
Error
}

Now, Run your page and check it.

No comments: