Write Html code In aspx page
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 th开发者_如何学编程ere any other way to write html code in to aspx page.
Id.InnerHtml = "html code";
its worked for me
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
CODE: PageWriter HttpHanlder
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
}
WEB.CONFIG
<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 ]
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");
}
Try this:
string newHtml = "...";
Response.Clear();
Response.Write(newHtml);
Response.End();
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);
}
精彩评论