I’m building a very simple ASP.NET user control to display messages with the help of jQuery UI styling.
For this, I built a separate web project, which is referenced by the site project.
I referenced it in web.config, like this:
<pages>
<controls>
<add tagPrefix="uc" namespace="Website.Main.UserControls" assembly="Website.Main.UserControls" />
</controls>
</pages>
Then in a test page, I instanced it like this:
<uc:StatusMessage ID="Test" runat="server" Type="Help" Text="testing.." />
Here’s the code for the control
using System;
namespace Website.Main.UserControls
{
public partial class StatusMessage : System.Web.UI.UserControl
{
public enum MessageType
{
Error,
Notice,
Help,
Info,
Success
}
public string Text
{
get { return Message.Text; }
set
{
Message.Text = value;
}
}
private MessageType _type;
public MessageType Type
{
get { return _type; }
set
{
_type = value;
if (_type == MessageType.Error)
{
MessageContainer.Attributes["class"] = "ui-icon ui-icon-alert";
MessageIcon.Attributes["class"] = "ui-state-error ui-corner-all";
}
else
{
MessageIcon.Attributes["class"] = "ui-state-highlight ui-corner-all";
if (_type == MessageType.Notice)
{
MessageContainer.Attributes["class"] = "ui-icon ui-icon-notice";
}
else if (_type == MessageType.Help)
{
MessageContainer.Attributes["class"] = "ui-icon ui-icon-help";
}
else if (_type == MessageType.Info)
{
MessageContainer.Attributes["class"] = "ui-icon ui-icon-info";
}
else if (_type == MessageType.Success)
{
MessageContainer.Attributes["class"] = "ui-icon ui-icon-check";
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetText(string text, MessageType type = MessageType.Info)
{
Text = text;
Type = type;
StatusMessagePanel.Update();
}
}
}
And the markup
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StatusMessage.ascx.cs" Inherits="Website.Main.UserControls.StatusMessage" %>
<asp:UpdatePanel ID="StatusMessagePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="ui-widget">
<div style="margin-top: 20px; padding-left: 7px; padding-top: 7px;" class="ui-state-highlight ui-corner-all" id="MessageContainer" runat="server">
<p>
<span style="float: left; margin-top: 4px; margin-right: .3em;" class="ui-icon ui-icon-info" id="MessageIcon" runat="server"></span>
<asp:Label ID="Message" runat="server"></asp:Label>
</p>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The problem is, whenever I get to the codebehind for this control, that is, right when the page renders and takes the attributes, all of the uc members are null. That is: Message, MessageContainer, MessageIcon and StatusMessagePanel are all null.
I have no clue what’s causing this behavior, I tried removing the update panel but nothing changes.
Using web.config file you can register only server web controls. But for user controls you must add register tag onto each page where those controls are used. Like this:
<%@ Register TagPrefix="uc" TagName="StatusMessage " Src="~/UserControls/StatusMessage .ascx" %>