views:

50

answers:

3

I have the following code:

   XElement Categories =
                       new XElement("Promotions",
                           from b in db.GetPromotions()
                        select new XElement("Promotion",
                            new XElement ("Category",b.CategoryName),
                               new XElement("Client",b.ClientName),
                               new XElement("ID",b.ID),
                               new XElement("Title",b.Title)));

                    XDocument mydoc = new XDocument();
                    mydoc.Add(Categories);

                    try
                    {
                            // Load the style sheet.
                            XslTransform xslt = new XslTransform();
                            xslt.Load(@"C:\Web\DesktopModules\Promotions\TransList.xslt");

                            // Execute the transform and output the results to a writer.
                            StringWriter sw = new StringWriter();
                            //XsltSettings mysettings = new XsltSettings();
                            XmlWriterSettings mysettings = new XmlWriterSettings();

                            xslt.Transform(mydoc.CreateReader(),null, sw);

                            String mstring = sw.ToString();

It generates the following string:

<ul id="red" class="treeview-red" xmlns:asp="http://schemas.microsoft.com/ASPNET/20"&gt;
  <li><span>Arts &amp; Entertainment</span><ul>
      <li><span>Client 1</span><ul>
          <li><span><asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkClicked" Text="Get your Free 2" /></span></li>
          <li><span><asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkClicked" Text="Get your Free 4" /></span></li>
          <li><span><asp:LinkButton ID="LinkButton5" runat="server" OnClick="LinkClicked" Text="Get your Free 5" /></span></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><span>Community &amp; Neighborhood</span><ul>
      <li><span>Client 2</span><ul>
          <li><span><asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkClicked" Text="Get your Free 1" /></span></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><span>Education</span><ul>
      <li><span>Client 3</span><ul>
          <li><span><asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkClicked" Text="Get Your Free 3" /></span></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><span>Home &amp; Garden</span><ul>
      <li><span>Client 4</span><ul>
          <li><span><asp:LinkButton ID="LinkButton6" runat="server" OnClick="LinkClicked" Text="Get your Free 6" /></span></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Now I take the string and add it to a panel which is part of a view in a multiview control:

Panel1.Controls.Add(new LiteralControl(mstring));

I have tried to play with Page.ParseControl, but I cannot get it to work right in the panel, the linkbuttons do not show, even though the text is there in the source. Any suggestions are quite welcome.

Thanks In Advance

Now I tried this:

Control myctrl = Page.ParseControl(mstring);         
Panel1.Controls.Add(myctrl);

and I get this as the one of the controls:

a id="dnn_ctr954_ViewPromotions_LinkButton2" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;dnn$ctr954$ViewPromotions$LinkButton2&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">Get your Free 2</a>

It doesn't work the way expected, dotnetnuke is doing some nutty stuff here using the parse control. it seems to drop my LinkClicked event.

A: 

Use ASP.NET XML control instead of using Literal control.

Description of server control:

An XML Web Server control is used to display the contents of an XML document without formatting or using XSL Transformations. You can optionally specify a XSLT style sheet that formats the XML document before it is displayed in an XML server control. The XML Web Server control belongs to the System.Web.UI.WebControls namespace. You can add an XML Web Server control to a Web form by dragging the control from the Web forms tab of the toolbox.

The XML Web Server control has the following properties:

DocumentSource: Allows you to specify the URL or the path of the XML document to be displayed in the Web form.

TransformSource: Allows you to specify the URL of the XSLT file, which transforms the XML document into the required format before it is displayed in the Web form.

Document: Allows you to specify a reference to an object of the XMLDocument class. This property is available only at runtime.

Transform: Allows you to specify a reference to an object of the XMLTransform class. This property is available only at runtime.

sashaeve
@sash I am not sure I understand what you mean by this. Instead of what?
James Campbell
Look here: http://www.beansoftware.com/ASP.NET-Tutorials/Using-XML.aspx
sashaeve
@sash hmm, cannot get this to work with an Xdocument. I will add that code also, should have mentioned that.
James Campbell
@sash I also was able to get to work, using the control you suggest, same behavior. It generates the proper HTML and the controls look fine in the source but they do not display in the browser.
James Campbell
How to: Load XML Data in the XML Web Server Control: http://msdn.microsoft.com/en-us/library/1atf1s3b(VS.80).aspx
sashaeve
@sash this is not xml and the xmlserver control does not work with an xdcoument nor xmlcompiledtransform. Let alone when I do get it to work, I get the same reqults i got in the start.
James Campbell
A: 

Code that you write in aspx files, such as

<asp:LinkButton ID="LinkButton1" runat="server">

never ends up in the final HTML!

Rather this markup is translated by the ASP.Net compiler into C#/.Net code which at runtime creates controls which in turn produce HTML output according to their state and data.

Adding asp markup into a Literal control will only result in the same markup, which then the browser is not able to display, since the tag asp:LinkButton is not defined in HTML. (Check by selecting "View Source" in your browser)

You may try to replace the asp:LinkButton (either in source mydoc or xslt) with a native HTML < button> tag, but I'm not sure how to distinguish between multiple buttons.

devio
yeah button won't work, but when I veiw source it looks fine. I cannot use a button as this is specific to the layout desired and is in a jquery control that would look horrible as a button.
James Campbell
+1  A: 

Well here ya go, this worked:

 String mstring = sw.ToString();
 var myctrl = ParseControl(mstring);

      foreach (Control Control in myctrl.Controls)
            {
                 if (Control is LinkButton)
                       {
                            LinkButton lb = (LinkButton)Control;
                            lb.OnClientClick = "LinkClicked";
                       }
                             Panel1.Controls.Add(myctrl);
            }
James Campbell