How do you clone 'WebControls in C # .NET?

My main question is: in .NET, how to clone WebControls?

I would like to create a custom tag that can create multiple copies of its children. Ultimately, I intend to create a tag similar to JSP / Struts.

But the first hurdle that I have is the ability to duplicate / clone the contents of the control.

Consider this rather contrived example:

<custom:duplicate count="2">
    <div>
        <p>Some html</p>
        <asp:TextBox id="tb1" runat="server" />
    </div>
</custom:duplicate>

The HTML markup that is output will be something like

<div>
    <p>Some html</p>
    <input type="text" id="tb1" />
</div>
<div>
    <p>Some html</p>
    <input type="text" id="tb1" />
</div>

Note. I know that I have a duplicate id, I can come up with this solution later!

So, we will have my user control with 3 children (I think) - a literal control, a TextBox control and another literal control.

"count = 2", , , / .

"OnInit", - :

List<WebControl> clones;
for(int i=1; i<count; i++)
{
    foreach(WebControl c in Controls) 
    {
        WebControl clone = c.Clone();
        clones.Add(clone);
    }
}

Controls.AddRange(clones);

, , WebControls ICloneable, .

, WebControls?

+3
4

dud. .

+4

, .

public class MyCustomServerCtrl
{

   ...

   public MyCustomServerCtrl Clone()
   {
      return MemberwiseClone() as MyCustomServerCtrl;
   }

}

: , , , , . , . , , , Repeater, ListView ..

+1

ASP.NET uses a way to create templates. There are samples for this on MSDN, just find the template controls / ITemplate.

0
source

The WebControl.CopyBaseAttributes method copies the AccessKey, Enabled, ToolTip, TabIndex, and Attributes properties from the specified web server control to the web server control from which this method is called.

MSDN Documentation

0
source

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


All Articles