How To Set User Control Property In .aspx Page?
User control can have one or more public properties used to manage its behavior and functionality. Let say you have a user control that shows a list of products from specific category. Public property CategoryID determines which category will be shown and have code like this:
[ C# ]
public
int CategoryID
{
get
{
if(!ViewState["CategoryID"]
== null)
return (int)ViewState["CategoryID"].ToString();
else
return 1; // default
value
}
set
{
ViewState["CategoryID"]
= value;
}
}
[ VB.NET ]
Public
Property CategoryID()
As Integer
Get
If Not ViewState("CategoryID")
= Nothing Then
Return Convert.ToInt32(ViewState("CategoryID"))
Else
'Default value
Return 1
End If
End
Get
Set(ByVal
value As Integer)
ViewState("CategoryID")
= value
End
Set
End
Property
To set this user control property in .aspx page, you can use this markup code:
<cc1:MyUserControl CategoryID="2" ID="ProductsView" runat="server"></cc1:MyUserControl>
Related articles:
1. ASP.NET Configuration System