UserControl constructor with parameters

My problem is when I want to use UserControl with a parameter.

it calls both constructors (constructor without parameter And constroctor with parameters)

Is this a normal situation?

If not, how do I build an 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 a simple userControl,

and name it on another page.

<%@ Register TagPrefix="uc" TagName="FreeExperience" Src="include/FreeExperience.ascx" %>

...

<uc:FreeExperience ID="ucFreeExperience" runat="server"/>

And in the function of Page_loadthis CS web page

ucFreeExperience = new Arche.Itempage3.include.FreeExperience(itemInfo);
+3
source share
2 answers

Do not use constructors for custom controls.

Open properties with get / set accessors.

+9
source
public FreeExperience(ItemInfo itemInfo) : this()

: this () calls your default constructor, if there is no specific reason why you are doing this, you can remove it.

, , , .

+1

Source: https://habr.com/ru/post/1746531/


All Articles