Search code examples
c#asp.netlinq-to-xmlmaster-pages

Display XDocument on a MasterPage


I have a MasterPage created using asp.net in C#. This contains my menus and so forth. I dynamically create an XDocument. Then I use:

Response.Write(outputDoc);

to display my XDocument as a webpage. It displays as expected, but it overwrites my MasterPage. I really want to put outputDoc in a container on my MasterPage but I can't find a way to do it.

I am completely lost on this. I must be using the wrong terminology, or trying to do this the completely wrong way because I can't find anything remotely relevant using Google.

Thanks for any assistance.


Solution

  • Maybe you can use the Literal control and set its content to the content of your outputDoc XDocument

    Code for .aspx page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Literal ID="Literal1" runat="server" />
            <asp:Literal ID="Literal2" runat="server" />
            <asp:Literal ID="Literal3" runat="server" />
        </div>
        </form>
    </body>
    </html>
    

    Code for .aspx.cs page

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Literal1.Mode = LiteralMode.Encode;
            Literal2.Mode = LiteralMode.PassThrough;
            Literal3.Mode = LiteralMode.Transform;
    
            Literal1.Text = @"<font size=4 color=red>Literal1 with Encode property example</font><script>alert(""Literal1 with the Encode property"");</script></br></br>";
    
            Literal2.Text = @"<font size=4 color=green>Literal2 with PassThrough property example</font><script>alert(""Literal2 with the PassThrough property"");</script></br></br>";
    
            Literal3.Text = @"<font size=4 color=blue>Literal3 with Encode Transform example</font><script>alert(""Literal3 with the Transform property"");</script></br></br>";
        }
    }
    

    Example and more details can be found here http://www.c-sharpcorner.com/uploadfile/puranindia/literal-control-in-Asp-Net/