S
S
Sergey2016-03-10 00:03:59
ASP.NET
Sergey, 2016-03-10 00:03:59

When adding a self-made control in WebForms to a form programmatically, the part that is written in XAML markup is lost, what to do?

Please tell me if you can.
For Web Forms I make custom controls, markup in a XAML file, code in C#.
Then I throw this element on the form - all the fields that were set when designing the user control in XAML - all appear on my form, in short, everything works.
If I try to add this control via code (C#), type
{ ... form.Controls.Add(new myControl()); ...}
then all these fields disappear (they become uninitialized).
Can anyone suggest what needs to be added?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2016-03-10
@dmitryKovalskiy

1) This is not XAML, but ASPX - otherwise you are developing for WPF and the question was asked in the wrong tags.
2) WebForms is dying crap, and if you plan to make a living with ASP.NET web development, learn MVC.
3) Your code in the described form is quite normal, but WebForms has a feature that few people want to mess with now, namely, the page life cycle. Attention a riddle - at what stage of a life cycle you vkryachit this kontrol? There is a possibility that you screwed it up at the stage when the page considers that all Controls are already fully initialized and it remains only to draw them.
4) Well, without the code of your control, it is also difficult to judge anything.

S
Sergey, 2016-03-10
Gorbulev

Hooray, there was an answer:
in the code instead of

WebUserControl myControl = new WebUserControl();
       form1.Controls.Add(myControl);

write
WebUserControl myControl = (WebUserControl)LoadControl("~/WebUserControl.ascx");
        form1.Controls.Add(myControl);

current in Page you need to register WebUserControl:
<%@ Register Src="~/WebUserControl.ascx" TagPrefix="мойКонтрол" TagName="WebUserControl" %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question