System.Collections.ListofTips

January 29, 2008

Archivos Embebidos en WebControls (ASP.NET 1.1)

Filed under: ASP.net, Resources — Tags: — José Franco @ 10:13 pm

Hace un tiempo atrás, me paso.. al intentar hacer mi primer WebControl… no saber como usar un archivo embebido, en donde trabajo no les agrada mucho la idea de tocar el webconfig, asi que no podia leerlo desde afuera, entonces lo que se me ocurrio, como necesitaba un javascript con ciertas funciones, era imprimir el archivo embebido…

Y aunque no lo crean, no encontraba demasiada informacion sobre esto…

Asi que lo comparto, es muy simple.. esto es sobre asp.net 1.1

Tenia mi WebCustomControl… y cree una carpeta llamada Scripts, con mi js, del tipo Embebido, y genere una Clase llamada Get

1 using System;
2 using System.Collections;
3
using System.Text;
4
using System.IO;
5
namespace WebControlLibrary1.Scripts
6 {
7 public class Get
8 {
9 private string _fileName;
10 public Get(string _file)
11 {
12 _fileName = _file;
13 }
14 public string GetContent()
15 {
16 StringBuilder _js = new StringBuilder();
17 _js.Append(“<scripts>” + System.Environment.NewLine);
18 StreamReader reader =
19 new StreamReader(typeof (Get).Assembly.GetManifestResourceStream(typeof (Get), _fileName));
20 string line;
21 while( (line=reader.ReadLine()) != null)
22 _js.Append(line + System.Environment.NewLine);
23
24
_js.Append(“</scripts>”);
25 return _js.ToString();
26 }
27 }
28 }

Y en mi webControl en el OnPreRender solo hace Page.RegisterClientScriptBlock, con el contenido que me retorna .Get(”file.js”);

Como veran, la clase se podria pasar a static directamente, ya que no tiene mucho sentido.. pero en un entonces, tenia mas logica en el contrucctor, y asi quedo…

Espero que a alguien le sea util, saludos.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • E-mail this story to a friend!
  • BarraPunto
  • blogmarks
  • co.mments
  • De.lirio.us
  • kick.ie
  • LinkedIn
  • Linkter
  • Live
  • Meneame
  • MyShare
  • Print this article!
  • TwitThis
  • YahooMyWeb
Bookmark and Share:

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress