Reputation: 20358
I have whole page HTML in my database and i want write that html code in to an aspx page. Means it replace old html code from new one.
Is there any other way to write html code in to aspx page.
Upvotes: 6
Views: 11053
Reputation: 1154
What kind of html is on the page? Does it have a masterpage? Why is there html on the page that you are replacing?
You could override the Render event of the page and write out the html.
protected override void Render(HtmlTextWriter writer)
{
writer.Write(newHtmlGoesHere);
}
Upvotes: 0
Reputation: 15799
You could put an <asp:Literal ...> control on the page and set the Text property to be the text from the database.
Or if you really what the whole page contents to come from a database, you could create an .ashx HttpHandler and do a Response.Write() to write the HTML back to the response stream. Something like this should work:
public void ProcessRequest(HttpContext context)
{
int id = 0;
if (context.Request.QueryString["id"] != null)
int.TryParse(context.Request.QueryString["id"], out id);
if (id != 0)
{
var con = new SqlConnection("myconnectionstring");
var cmd = new SqlCommand("SELECT html FROM mytable WHERE id = @id", con);
cmd.Parameters.Add("id", id);
object html = cmd.ExecuteScalar();
if (html != null)
context.Response.Write(html);
}
else
context.Response.Write("No content found");
}
Upvotes: 2
Reputation: 43217
Use HttpHandler to do this. Just read the HTML page from the database and flush it to response stream.
Be sure to map the handler with a route in web.config
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for PageWriter
/// </summary>
public class PageWriter : IHttpHandler
{
public PageWriter()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
try
{
string htmlPageFromDatabase = string.Empty;
//Read the HTML Page content into htmlPageFromDatabase from database
context.Response.Write(htmlPageFromDatabase);
}
catch (Exception ex)
{
context.Response.Write("Page could not be loaded due to " + ex.Message);
}
}
#endregion
}
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add path="PageFromDatabase.aspx" verb="GET" type="PageWriter" />
</httpHandlers>
After this, You cann access your page from http://yourserver/yourapp/PageFromDatabase.aspx [ ex: http://localhost/mywebsite/PageFromDatabase.aspx ]
Upvotes: 4
Reputation: 57996
Try this:
string newHtml = "...";
Response.Clear();
Response.Write(newHtml);
Response.End();
Upvotes: 0