Search code examples
c#asp.netweb-controls

How can I contain the size of my literal control?


I'm working on an ASP.NET site and I'm using HttpGetRequest & HttpGetResponse to place the contents of https://news.google.com/news into a literal control on one of my pages.

The problem is that the stream is covering the entire page. I can't manage to control the size with a div or container. So the stream from the literal is covering a label that is above it (in its own div) and a menu that's inherited from SiteMaster.Master.

How can I keep the menu and label above the literal control? Code behind:

        protected void Page_Load(object sender, EventArgs e)
    {
        HttpWebRequest googRequest = (HttpWebRequest)HttpWebRequest.Create("https://news.google.com/news");
        HttpWebResponse resp = (HttpWebResponse)googRequest.GetResponse();
        string googNews = new System.IO.StreamReader(resp.GetResponseStream()).ReadToEnd();

        Literal1.Text = googNews;

        Label1.Text = DateTime.Now.ToLongTimeString();
    }

ASP:

<%@ Page Title="" Language="C#" MasterPageFile="~/SiteMaster.Master" AutoEventWireup="true" CodeBehind="Literally.aspx.cs" Inherits="DNut.Literally" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <p>
        <asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="Larger" Font-Italic="true"></asp:Label>
    </p>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
        <div>
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        </div>
</asp:Content>


Solution

  • The content, based on how you are receiving it, will likely include an html tag, head tag and body tag. This isn't valid markup to inject into the middle of an existing html document. The typical way to include a whole page like this in another html document is via an iframe tag.

    Iframe's are frowned on a bit in the web dev world and not used alot but in this case it's its a good fit for what you are trying to do. It's also helps protect your site against Cross-site scripting (XSS) attacks (verses trying to inject markup into a div via literal tag.

    With an iframe tag you set the 'src' attribute to the url you want to display and you won't need the literal or the related c# code to set it.

     <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
        <iframe src='https://news.google.com/news'></iframe>
     </asp:Content>
    

    You can style up the iframe to have a desirable height and width.