[Fwd: Re: [Mono-list] Problem with System.Web.UI/TemplateControl
method]
javier goday
jgoday@enmacosa.com
Mon, 02 May 2005 09:00:31 +0200
> Hello,
> I see that ParseControl method from
> mcs/class/System.Web/System.Web.UI/TemplateControl.cs
> is not yet implemented,
> ( Control ctrl = Page.ParseControl("<asp:button ..."); )
>
> anybody can say me what is the state of this method or when will be
> implemented ??
>
> Is there any alternative for create Controls from a string ??
>
> Thanks , and sorry for my bad english !!
i think this may be work fine
please, post any suggestion
public class Form : System.Web.UI.UserControl {
protected Panel pnlContainer;
private string xmlFile="";
private string xslFile="/Common/Xslt/Forms/Default.xsl";
static Logger logger =
LogManager.GetLogger("FormControl.Form");
public string XmlFileName {
set {
this.xmlFile=value;
}
get {
return this.xmlFile;
}
}
public string XslFileName {
set {
this.xslFile=value;
}
get {
return this.xslFile;
}
}
public void Page_Load(object sender, System.EventArgs e)
{
string XmlSystemFileName = Server.MapPath(xmlFile);
string XslSystemFileName =
Server.MapPath(xslFile);
XPathDocument XmlDoc = new
XPathDocument(XmlSystemFileName);
XslTransform XslProc = new XslTransform();
XsltArgumentList XsltArgs = new
XsltArgumentList();
XsltArgs.AddParam("pageid", "", "pag1");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XslProc.Load(XslSystemFileName);
XslProc.Transform(XmlDoc, XsltArgs, sw,null);
String result=
sb.ToString().Replace("<","<").Replace(">",">");
sw.Close();
byte [] bytesbuffer =
Encoding.ASCII.GetBytes(result);
MemoryStream memStream = new MemoryStream(bytesbuffer);
this.ParseControl(memStream);
}
private void ParseControl(Stream doc) {
XmlDocument myXmlDocument = new XmlDocument();
try {
myXmlDocument.Load(doc);
ParseTree(myXmlDocument.DocumentElement);
}
catch (Exception e) {
logger.Debug("# FormControl.Form.ParseControl ERROR :>
{0}",e.Message);
Console.WriteLine("# ERROR :> " + e.Message);
}
}
private void ParseTree(XmlNode node) {
Control ctrl;
if (node != null) {
if (node.HasChildNodes == true) {
if(node.Name.StartsWith("asp:")) {
ctrl=this.BuildControl(node);
}
else {
ctrl = new Literal();
String strCtrl = "<"+node.Name;
if (XmlNodeType.Element == node.NodeType){
XmlNamedNodeMap map =
node.Attributes;
foreach (XmlNode attrNode in map){
strCtrl+=" "+attrNode.Name+"=
\""+attrNode.Value+"\"";
}
}
strCtrl+="> "+node.Value;
((Literal)ctrl).Text=strCtrl;
pnlContainer.Controls.Add(ctrl);
XmlNode nodeChild =
node.FirstChild;
while (nodeChild != null){
ParseTree(nodeChild);
nodeChild = nodeChild.NextSibling;
}
ctrl = new Literal();
((Literal)ctrl).Text="</"+node.Name + " >";
}
pnlContainer.Controls.Add(ctrl);
}
else {
if(node.Name.StartsWith("asp:")) {
ctrl=this.BuildControl(node);
}
else {
ctrl = new Literal();
((Literal)ctrl).Text=node.Value;
}
pnlContainer.Controls.Add(ctrl);
}
}
}
private Control BuildControl(XmlNode node) {
Control ctrl;
try{
Type type=
Type.GetType("System.Web.UI.WebControls."+node.Name.Replace("asp:","")+",System.Web",true,true);
ctrl = (Control)
Activator.CreateInstance(type);
XmlAttributeCollection
xmlAttributeCollection = node.Attributes;
foreach(XmlAttribute xAttr in
xmlAttributeCollection) {
PropertyInfo pInfo =
type.GetProperty(xAttr.Name);
if(pInfo.PropertyType.IsEnum) {
pInfo.SetValue(ctrl,Enum.Parse(pInfo.PropertyType,xAttr.Value),null);
}
else {
pInfo.SetValue(ctrl,xAttr.Value,null);
}
}
return ctrl;
}
catch(TypeLoadException ex) {
ctrl = new Label();
((Label)ctrl).Text="Non existe o tipo
"+node.Name;
return ctrl;
}
catch(Exception e) {
logger.Debug("# ERROR
FormControlForm.BuildControl :>
{0}",e.Message);
return new Control();
}
}
}