2010-03-15 11 views
9

में एक से अधिक नियंत्रण संग्रह को प्रस्तुत करें। कभी कई नियंत्रण संग्रह के साथ एक नियंत्रण का निर्माण करने की कोशिश की, लेकिन (सरलीकृत) इस तरह इसे हल:मैं एक कस्टम Webcontrol है, जो निम्नलिखित संरचना है का निर्माण किया है ASP.NET कस्टम नियंत्रण

[PersistChildren(false), ParseChildren(true)] 
public class ModalBox : WebControl 
{ 
    private ControlCollection _contents; 
    private ControlCollection _footer; 

    public ModalBox() 
     : base() 
    { 
     this._contents = base.CreateControlCollection(); 
     this._footer = base.CreateControlCollection(); 
    } 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    public ControlCollection Contents { get { return this._contents; } } 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    public ControlCollection Footer { get { return this._footer; } } 

    protected override void RenderContents(HtmlTextWriter output) 
    { 
     // Render content controls. 
     foreach (Control control in this.Contents) 
     { 
      control.RenderControl(output); 
     } 

     // Render footer controls. 
     foreach (Control control in this.Footer) 
     { 
      control.RenderControl(output); 
     } 
    } 
} 

यह ठीक से प्रस्तुत करने लगता है हालांकि, यह अब और अगर मैं कुछ asp.net लेबल और इनपुट जोड़ने काम नहीं करता है संपत्ति के अंदर नियंत्रण (एएसपीनेट कोड ऊपर देखें)। मैं HttpException मिल जाएगा:

आईडी 'KeywordTextBox' कि लेबल साथ जुड़ा हुआ है के साथ नियंत्रण प्राप्त करने में असमर्थ 'KeywordLabel'।

कुछ हद तक समझ में आता है, लेबल controlcollection में पाठ बॉक्स से पहले दिखाई देता है क्योंकि। हालांकि, डिफ़ॉल्ट asp.net नियंत्रण के साथ यह काम करता है, तो यह क्यों काम नहीं करता है? मैं क्या गलत कर रहा हूं? क्या एक नियंत्रण में दो नियंत्रण संग्रह भी संभव है? क्या मुझे इसे अलग-अलग प्रस्तुत करना चाहिए?

उत्तर के लिए धन्यवाद।

उत्तर

2

आप अपने दो के माता-पिता के रूप में दो पैनलों का उपयोग कर सकते नियंत्रण संग्रह (और वे समूहकरण और बेहतर पठनीयता प्रदान करेंगे)। प्रत्येक संग्रह से अपने नियंत्रण को संबंधित पैनल के नियंत्रण संग्रह में जोड़ें, और रेंडर विधि में बस प्रत्येक पैनल के रेंडर विधियों को कॉल करें। पैनल स्वचालित रूप से अपने बच्चों को प्रस्तुत करेंगे, और उन्हें अपना नामस्थान प्रदान करेंगे, इसलिए, आप विभिन्न पैनलों में समान आईडी के साथ नियंत्रण कर सकते हैं।

+0

हाँ यह काम करेगा! –

1

मुझे यकीन नहीं है कि यह काम करेगा। यदि आप टेम्पलेट का उपयोग करते हैं, तो आप आउटपुट को सही ढंग से प्रस्तुत करने के लिए नियंत्रण प्राप्त कर सकते हैं।

सबसे पहले, एक वर्ग को परिभाषित कंटेनर नियंत्रण के लिए प्रकार के रूप में प्रयोग की जाने वाली:

public class ContentsTemplate : Control, INamingContainer 
{ 
} 

और अब कस्टम नियंत्रण:

[PersistChildren(false), ParseChildren(true)] 
public class ModalBox : CompositeControl 
{ 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [TemplateContainer(typeof(ContentsTemplate))] 
    public ITemplate Contents { get; set; } 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [TemplateContainer(typeof(ContentsTemplate))] 
    public ITemplate Footer { get; set; } 

    protected override void CreateChildControls() 
    { 
    Controls.Clear(); 

    var contentsItem = new ContentsTemplate(); 
    Contents.InstantiateIn(contentsItem); 
    Controls.Add(contentsItem); 

    var footerItem = new ContentsTemplate(); 
    Footer.InstantiateIn(footerItem); 
    Controls.Add(footerItem); 
    } 

} 
+0

मैं ओप के समान समस्या मार रहा हूं और यह काम नहीं करेगा। आप ITEMplates के अंदर किसी भी नियंत्रण का संदर्भ नहीं दे सकते क्योंकि उन्हें ठीक से तत्काल नहीं किया जाता है, जो इस प्रकार के नियंत्रण के उद्देश्य को हरा देता है। – mattmanser