Visual Studio .NET - Displaying content of a html file on literal control asp.net 2.0 with C#

Asked By shekhar kumar on 04-Nov-08 07:04 AM
I am using literal control to display data of a html file.
I am wring the code as follows
System.IO.StreamReader SR = new System.IO.StreamReader("filename.html");
Literal1.text=SR.ReadToEnd();

But is showing some boxes at the asp.net page.
Please help me.

Thanks in advance.

Text property literal control - Venkat K replied to shekhar kumar on 04-Nov-08 09:19 AM

Text: The caption displayed in the Literal control. This text is passed directly to the browser page when rendered by ASP.NET.

There is not chance of getting border for using the text property. may be by mistake you may have used some styles "cssclass" to that literal control. Re-check your html code once.

Here is the solution - Binny ch replied to shekhar kumar on 04-Nov-08 11:44 AM

In this case the Literal control is named "Literal1":
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path = Server.MapPath("/") + "HTMLPage.htm";
 
        using (StreamReader reader = File.OpenText(path))
        {
            string fileContents = reader.ReadToEnd();
 
            this.Literal1.Text = fileContents;
        }
    }
}

try this code for literal control - C_A P replied to shekhar kumar on 05-Nov-08 03:07 AM

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class GridViewComplexHead : System.Web.UI.Page
{
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Class", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Literature", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Math", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("English", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Computer", typeof(System.Decimal)));

for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i);
dr = dt.NewRow();
dr[0] = "Class" + i.ToString();
dr[1] = "Student" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);

Literal newCells = new Literal();
newCells.Text = @"Table Head letter1</th>
<th colspan='2'>Table Head letter2</th>
<th colspan='2'>Table Head letter3</th>
<th>Table Head letter4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>Table Head letter5</th>
<th rowspan='2'>Table Head letter6</th>
<th colspan='2'>Table Head letter7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>Table Head letter8</th>
<th>Table Head letter9</th>
<th>Table Head letter10</th>
<th>Table Head letter11</th>
<th>Table Head letter12";

TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();

headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);

rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;

GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px");
}
else
{
e.Row.Attributes.Add("style", "background:#FFF");
}
}
}
literal control - C_A P replied to shekhar kumar on 05-Nov-08 03:10 AM

Here's an example that uses the Literal control:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Working with Literal Web control</title>
</head><body>
<form id="frmLiteralControl" runat="server">
<asp:Literal ID="Assigned_ID" Text="Text to display"
mode="PassThrough" runat="server" />
</form></body></html>


more info available on

http://www.builderau.com.au/program/asp/soa/Use-ASP-NET-s-Literal-control-to-its-full-potential/0,339028371,339286810,00.htm