I develop a custom usercontrol. For a specific need, I have to get the html generate from usercontrol inside it and display only it in the page's body.
I think it's possible overloading RenderControl(HtmlTextWriter writer) but I don't know how.
Thanks
You can render the control in memory, have it on a string, and then print this string somewhere on your page as you say. Here is the code that loads the control and renders it on memory, then have the results on a string.
// load the control
var oTesto = Page.LoadControl("Testo.ascx");
// here you need to run some initialization of your control
// because the page_load is not loading now.
// a string writer to write on it
using(TextWriter stringWriter = new StringWriter())
{
// a html writer
using(HtmlTextWriter GrapseMesaMou = new HtmlTextWriter(stringWriter))
{
// now render the control inside the htm writer
oTesto.RenderControl(GrapseMesaMou);
// here is your control rendered output.
strBuild = stringWriter.ToString();
}
}
To capture the rendering of your control you can use the Render as:
protected override void Render(HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
// now the control is inside the htmlWriter as final rendered text
base.Render(htmlWriter);
// here is how to make the control render itself
// base.Render(writer);
}
You place the render inside your control class.
Related
I have this code:
public static string RenderView(string path)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
pageHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
Which is run from:
[WebMethod]
public string GetReportsHTML()
{
string output = "";
output = ViewManager.RenderView("ReportsControl.ascx");
return output;
}
This is to test rendering ASCX files and spitting them out of a SOAP/REST service.
Problem is, some controls (runat=server ones) fail if they are not encapsulated in a tag with runat=server.
The solution to that is here, but the solution assumes being inside an ASPX file where I can just edit the markup.
How would I programmatically build a Page, add a Form, set runat=server so that I can follow that solution and add my control to the Form Control?
Have you tried something like this ?
public static string RenderView(string path)
{
Page pageHolder = new Page();
System.Web.UI.HtmlControls.HtmlForm formHolder = new System.Web.UI.HtmlControls.HtmlForm();
pageHolder.Controls.Add(formHolder );
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
formHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
Hope this will help
I have a new requirement to have a button in my web page, and upon clicking, it displays the web page as a PDF. On researching I found that iTextSharp is used for such purpose.
But I have many Telerik and ASP controls in my web page too. Can I get that also in the PDF using iTextSharp?
If yes then can anyone please provide me a basic link to start using iTextSharp as I haven't came across this tool yet.
According to the answer from #ahaliav I tried with the following code. However, I'm not getting the controls on the pdf.
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(HtmlContent);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
HTMLWorker worker = new HTMLWorker(document);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
// step 3: we create a worker parse the document
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
//Response.Write(document);
//Response.End();
}
protected override void Render(HtmlTextWriter writer)
{
// setup a TextWriter to capture the markup
TextWriter tw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
// render the markup into our surrogate TextWriter
base.Render(htw);
// get the captured markup as a string
string pageSource = tw.ToString();
// render the markup into the output stream verbatim
writer.Write(pageSource);
// remove the viewstate field from the captured markup
HtmlContent = Regex.Replace(pageSource,
"<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
"", RegexOptions.IgnoreCase);
// the page source, without the viewstate field, is in viewStateRemoved
// do what you like with it
}
Please help .
I have other option also,that is to display the image of the full web page when i click a button.Can anyone guide me to any possible directions for these two issues please.
I finally changed my code as:
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
MemoryStream msOutput = new MemoryStream();
With this i am able to get the msword document containing all my controls ,but i am also getting some unwanted texts,how can i remove that,if anyone encountered the same problem?
After some research i did not find any solution for showing the controls in the html this code below works fine but with out showing the controls,
in the past i used this http://www.winnovative-software.com/ and it works good for html pages including all controlls you can also test it onlie, the "only" problem is it you need to pay for the license
protected void btnExportToPdf_Click(object sender, EventArgs e)
{
renderPDF = true;
}
protected override void Render(HtmlTextWriter writer)
{
if (renderPDF == true)
{
MemoryStream mem = new MemoryStream();
StreamWriter twr = new StreamWriter(mem);
HtmlTextWriter myWriter = new HtmlTextWriter(twr);
base.Render(myWriter);
myWriter.Flush();
myWriter.Dispose();
StreamReader strmRdr = new StreamReader(mem);
strmRdr.BaseStream.Position = 0;
string pageContent = strmRdr.ReadToEnd();
strmRdr.Dispose();
mem.Dispose();
writer.Write(pageContent);
CreatePDFDocument(pageContent);
}
else
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
base.Render(tw);
// get the captured markup as a string
string pageSource = tw.ToString();
//Get the rendered content
string sContent = sb.ToString();
//Now output it to the page, if you want
writer.Write(sContent);
}
}
public void CreatePDFDocument(string strHtml)
{
string strHTMLpath = Server.MapPath("MyHTML.html");
StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
strWriter.Write(strHtml);
strWriter.Close();
string strFileName = HttpContext.Current.Server.MapPath("map1.pdf"); // strFileName "C:\\Inetpub\\wwwroot\\Test\\map1.pdf"
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
TextReader tr = new StreamReader(Server.MapPath("MyHTML.html"));
//add the collection to the document
document.Open();
/////////
iTextSharp.text.html.simpleparser.HTMLWorker worker = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
worker.StartDocument();
//// step 5: parse the html into the document
worker.Parse(tr);
//// step 6: close the document and the worker
worker.EndDocument();
//worker.Parse(tr); // getting error "Illegal characters in path"
document.Close();
ShowPdf(strFileName);
}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}
A very simple Google search brings us to the following forum thread which has your "sollution":
http://forums.asp.net/t/1039486.aspx/1
iTextSharp comes with a little companion : XML Worker
For a demo, have a look here
Even though the documentation refers to the Java API, the adaptation to C# should be straightforward.
As for Telerik/ASP tags, you can extend XMLWorker and define how to convert such tags into PDF elements.
I am trying to get the rendered HTML of Ajaxtoolkit:Editor control. I am getting an error while rendering here is my code:
[WebMethod]
public static string GetHtmlEditor()
{
CustomHTMLEditor objCustom = new CustomHTMLEditor();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter textWriter = new HtmlTextWriter(sw);
UpdatePanel up = new UpdatePanel();
up.ID = "upId";
up.ContentTemplateContainer.Controls.Add(objCustom);
up.RenderControl(textWriter);
return textWriter.ToString();
}
Error: Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.
I tried executing directly i.e without using UpdatePanel. Still it throws the same error.
Can i render Ajaxtoolkit controls?
i want to load a user control using jquery ajax. One possible i found is to load usercontrol through generic handler. Anyone help me plsss. here the ajax code i am using to call the control.
<script type="text/javascript">
function fillSigns() {
$.ajax({
url: "usercontrolhandler.ashx?control=signs.ascx",
context: document.body,
success: function (data) {
$('#signdiv').html(data);
}
});
}
</script>
and here is the code in handler file
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Page page = new Page();
UserControl ctrl = (UserControl)page.LoadControl("~/" + context.Request["control"] + ".ascx");
page.Form.Controls.Add(ctrl);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(stringWriter);
ctrl.RenderControl(tw);
context.Response.Write(stringWriter.ToString());
}
This code raises object reference not found error at below shown line.
page.Form.Controls.Add(ctrl);
It seems page.Form is null here, that's why you've got a null reference exception. You could add your user control to the page's control collection instead:
page.Controls.Add(ctrl);
You could also use HttpServerUtility.Execute method for page rendering:
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(page, output, false);
And finally take a look onto Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios article by Scott Guthrie which covers your problem.
Try this:
Page page = new Page {ViewStateMode = ViewStateMode.Disabled};
HtmlForm form = new HtmlForm { ViewStateMode = ViewStateMode.Disabled };
form.Controls.Add(ctrl);
page.Controls.Add(form);
then:
StringWriter stringWriter = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(stringWriter);
page.RenderControl(tw);
context.Response.Write(stringWriter.ToString());
I want to use an ascx as a template, and render it programatically, using the resulting html as the return value of an ajax method.
Page pageHolder = new Page();
MyUserControl ctlRender = (MyUserControl)pageHolder.LoadControl(typeof(MyUserControl),null);
pageHolder.Controls.Add(ctlRender);
System.IO.StringWriter swOutput = new System.IO.StringWriter();
HttpContext.Current.Server.Execute(pageHolder, swOutput, false);
return swOutput.ToString();
This all executes, and the Page Load event of the user control fires, but the StringWriter is always empty. I've seen similar code to this in other examples, what am I missing?
Have you tried this:
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
Taken directly from the article here:
ASP.NET Tip: Render Control into HTML String
You always have to use something like this.Controls.Add(TemplateControl.LoadControl("~/PathTo/YourControl.ascx")
The reason is, that there is no internal mapping of Types to there ascx-file (only the other way arround). So this means if you initialize new YourControl(), it will not do anything you defined in the ascx part. If you would have
protected override void protected override void Render(HtmlTextWriter output) {
output.WriteLine("Test");
}
this would give you "Test" in the place you rendered your control to.