ASP.NET Custom Controls - Composites

后端 未结 6 514
别跟我提以往
别跟我提以往 2020-12-13 20:13

Summary

Hi All,
OK, further into my adventures with custom controls...

In summary, here is that I have learned of three main "classes" of cus

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 21:04

    I often use composite controls. Instead of overriding Render or RenderContents, just assign each Control a CssClass and use stylesheets. For multiple Controls.Add, I use an extension method:

    //Controls.Add(c1, c2, c3)
    static void Add(this ControlCollection coll, params Control[] controls)
     { foreach(Control control in controls) coll.Add(control);
     }
    

    For quick and dirty rendering, I use something like this:

    writer.Render(@"
    {0}
    ", Text); control1.RenderControl(writer); writer.Render("
    ");

    For initializing control properties, I use property initializer syntax:

    childControl = new Control {  ID="Foo"
                                , CssClass="class1"
                                , CausesValidation=true;
                               };
    

提交回复
热议问题