ASP.NET Custom Controls - Composites

后端 未结 6 530
别跟我提以往
别跟我提以往 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 20:51

    Here's another extension method that I use for custom rendering:

     public static void WriteControls
            (this HtmlTextWriter o, string format, params object[] args)
     { 
        const string delimiter = "<2E01A260-BD39-47d0-8C5E-0DF814FDF9DC>";
        var controls  = new Dictionary();
    
        for(int i =0; i < args.Length; ++i)
        { 
           var c = args[i] as Control; 
           if (c==null) continue;
           var guid = Guid.NewGuid().ToString();
           controls[guid] = c;
           args[i] = delimiter+guid+delimiter;
        }
    
        var _strings = string.Format(format, args)
                             .Split(new string[]{delimiter},
                                    StringSplitOptions.None);
        foreach(var s in _strings)
        { 
           if (controls.ContainsKey(s)) 
               controls[s].RenderControl(o);
           else 
               o.Write(s);
        }
    }
    

    Then, to render a custom composite in the RenderContents() method I write this:

    protected override void RenderContents(HtmlTextWriter o)
    { 
        o.WriteControls
             (@"
    {0} {1}
    " ,Text ,control1); }

提交回复
热议问题