Like standard ASP.NET built-in server controls, user controls can have properties (attributes) on its definition tag. Suppose you want to add color effect on UserControl.ascx
file like this:
<uc:UserControl ID="UserControl1" runat="server" Color="blue" />
At this point, custom attributes/properties for user controls can be set by declaring properties inside user control's code behind:
private String _color;
public String Color
{
get
{
return _color;
}
set
{
_color = value;
}
}
Additionally, if you want to set default value on a user control property, assign the default value inside user control's constructor method.
public UserControl()
{
_color = "red";
}
Then, user control markup should be modified to add color attribute as following example:
<%@ Control Language="C#" AutoEventWireup="True" CodeFile="UserControl.ascx.cs" %>
<div>
<span style="color:<%= Color %>"><asp:Label ID="Label1" runat="server" /></span>
<br />
<asp:Button ID="Button1" runat="server" Text="Click Here" OnClick="Button1_Click" />
</div>