UserControl Constructor with parameters
My Problem is
when I want to use UserControl with parameter.it call both constructor( constructor with no parameter And constroctor with parameters)
is this normal situation??
if not, how should I construct the object.
public partial class FreeExperience : Arche.Web.UI.UserControlBase
{
private ItemInfo itemInfo;
public FreeExperience() : base()
{
}
public FreeExperience(ItemInfo itemInfo) : this()
{
this.itemInfo = itemInfo;
}
here I made simple userControl ,
and Call it like this on the another page.
<%@ Register TagPrefix="uc" TagName="FreeExperience" Src="include/FreeExperience.ascx" %>
...
<uc:FreeExperience ID="ucFreeExperience" runat="serv开发者_开发知识库er"/>
And On the Page_load
function of this webpage's CS
ucFreeExperience = new Arche.Itempage3.include.FreeExperience(itemInfo);
Dont use constructors on user controls.
Expose properties with get/set accessors.
public FreeExperience(ItemInfo itemInfo) : this()
:this() is calling your default constructor, if there is no specific reason why you are doing so, you can remove it.
Actually, I always operate the other way around, and have constructors with little or no parameter, call a parametered constructor, providing default values.
精彩评论