2010-10-15 17 views
6

मैं एक एम्बेडेड संसाधन को ITemplate के रूप में कैसे लोड कर सकता हूं? LoadTemplate() विधि केवल एक स्ट्रिंग वर्चुअल पथ लेती है, और जाहिर है यह एम्बेडेड संसाधनों के लिए काम नहीं करेगा।एम्बेडेड संसाधन से लोड टेम्पलेट

+0

आप समझा सकते हैं जहां 'ITemplate' से आता है? – JaredPar

+0

@JaredPar, asp.net - मैंने टैग को शामिल करने के लिए संशोधित किया। –

+0

प्रत्येक फ़ाइल में एक पथ है। क्या आप एक डीएलएल के अंदर एक फाइल तक पहुंचने की कोशिश कर रहे हैं? – BrunoLM

उत्तर

2

मान लें कि आपके टेम्पलेट्स एम्बेडेड हैं और मुझे इस तरह रहने की आवश्यकता है (जो मुझे लगता है कि आप पुनर्विचार करना चाहते हैं), यहां एक ऐसा फ़ंक्शन है जिसे मैंने कुछ समय पहले लिखा था कि मैंने एम्बेडेड फ़ाइलों से निपटने के दौरान सफलतापूर्वक कई बार उपयोग किया है (ज्यादातर। एसक्यूएल फाइलें)। यह एक एम्बेडेड संसाधन को एक स्ट्रिंग में परिवर्तित करता है। फिर आपको डिस्क पर अपना टेम्पलेट लिखने की आवश्यकता हो सकती है।

public static string GetEmbeddedResourceText(string resourceName, Assembly resourceAssembly) 
{ 
    using (Stream stream = resourceAssembly.GetManifestResourceStream(resourceName)) 
    { 
     int streamLength = (int)stream.Length; 
     byte[] data = new byte[streamLength]; 
     stream.Read(data, 0, streamLength); 

     // lets remove the UTF8 file header if there is one: 
     if ((data[0] == 0xEF) && (data[1] == 0xBB) && (data[2] == 0xBF)) 
     { 
     byte[] scrubbedData = new byte[data.Length - 3]; 
     Array.Copy(data, 3, scrubbedData, 0, scrubbedData.Length); 
     data = scrubbedData; 
     } 

     return System.Text.Encoding.UTF8.GetString(data); 
    } 
} 

उदाहरण उपयोग:

var text = GetEmbeddedResourceText("Namespace.ResourceFileName.txt", 
            Assembly.GetExecutingAssembly()); 
+0

मैं चाहता था कि टेम्पलेट को एम्बेड किया जाए क्योंकि इसे नियंत्रण के लिए डिफ़ॉल्ट टेम्पलेट के रूप में उपयोग किया जाता है, और यह एक विशिष्ट शैली का हिस्सा है। – MadSkunk

0

आपका नियंत्रण यह तरह दिखना चाहिए:

public class Con : Control 
{ 
    public Template Content { get; set; } 

    protected override void CreateChildControls() 
    { 
     base.CreateChildControls(); 

     Content = new Template(); 

     // load controls from file and add to this control 
     Content.InstantiateIn(this); 
    } 

    public class Template : ITemplate 
    { 
     public void InstantiateIn(Control container) 
     { 
      // load controls 
      container.Controls.Add((HttpContext.Current.Handler as Page).LoadControl("Emb.ascx")); 
     } 
    } 
} 

फिर एम्बेडेड फ़ाइल:

<%@ Control Language="C#" %> 

<asp:TextBox ID="Tb" runat="server" /> 

तब नियंत्रण का उपयोग जब यह एम्बेडेड संसाधन लोड होगा, इसलिए का उपयोग कर:

<%@ Register Assembly="TestWeb" Namespace="TestWeb" TagPrefix="c" %> 
<c:Con runat="server" /> 

टेक्स्टबॉक्स बनाएगा।


आप, see this implementation of VirtualPathProvider एक DLL के अंदर एक फ़ाइल का उपयोग करने की कोशिश कर रहे हैं।

संबंधित मुद्दे