What is the correct way to initiate user control from code

Basically, I want to create a user control in the code behind, DataBind (), and then insert the control into the current page

I am currently trying to do this:

var comment = new IncidentHistoryGroupComment();
comment.LoadControl("~/Controls/IncidentHistoryGroupComment.ascx");
comment.LoadTemplate("~/Controls/IncidentHistoryGroupComment.ascx");
comment.InitializeAsUserControl(this);
comment.AttachmentActions = group.HastAttachmentActions ? group.AttachmentActions : null;
comment.Comment = group.Comment;
comment.NextStep = group.NextStep;
comment.IsInitiationStep = group.InitializationEntry != null;
comment.DataBind();

But all the controls inside it are zero. For example, I have a panel with id pnlComments, and when I try to access it in the IncidentHistoryGroupComment.DataBind () method, I get null. I also checked the Controls property to see if there is something there, but Controls.Count == 0

So the question is how to properly initialize the user control from the code so that all the controls are assigned to their instances in IncidentHistoryGroupComment.designer.cs so that I can easily access them.

+3
3

- :

 var comment = (IncidentHistoryGroupComment)Page.LoadControl("~/Controls/IncidentHistoryGroupComment.ascx");

+1
MyControl myControl = (MyControl)LoadControL("~/MyControl.ascx");

.

+2

, .

DataBind :

this.EnsureChildControls();

, . , createChildControl, .

0

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


All Articles