LoadControl in WCF - c#

Since i havn't access to the TemplateControl or page from a WCF service i was wondering if it was possible to render a custom control? If so how would one do it?
private string GetRenderedHtmlFrom(Control control)
{
StringBuilder stringBuilder = new StringBuilder();
StringWriter sw = new System.IO.StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(textWriter);
control.RenderControl(htmlWriter );
return stringBuilder.ToString();
}
Thanks

This actually wasn't achievable and i ended up abandoning the idea. The rough solution i implemented was loading an html page, and using string.Format() to manipulate it then returned the results as a string and let the JavaScript 'load the control'.

Related

How retrieve by c# page html generated with AngularJS

I need retrieve the HTML code for a page that uses the AngularJS to process some information and generate a graph. I could easily retrieve the html code using WebRequest, as the example below, but the content (graphic) generated by AngularJS does not come in the page code.
WebRequest request = WebRequest.Create("http://localhost:36789/minhaapp#/index");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
html = sr.ReadToEnd();
}
Has anyone ever experienced this?
Thank you in advance for your support.
At the end of your Page_Load method, call this getHTMLContent() method:
public string getHTMLContent()
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
panel.RenderControl(hw);
String html = sb.ToString();
return html;
}
The entire page is contained in an asp:Panel called panel. The way this works is from the RenderControl() method which you can read a little bit more about here. Simply put, it gets all the content within the asp:Panel tags (the whole page) and once used after the Page_Load event has been executed, it will get all the raw HTML for the page.
There's a library called PhantomJS, which renders the js off the site first and then u can get the source after it got rendered. But obviously it will also slow down the process if you are doing a lot of websites

Serialization between different domains

I'm new to serialization.I need to send a Serialized object from a one web site to another web site.
i'm using following serialization code in my first web site
private void Serialize()
{
Cuser cat = new Cuser();
cat.UserNO = 1;
cat.UerName = "chamara";
cat.Passwod = "123";
XmlSerializer ser = new XmlSerializer(cat.GetType());
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
ser.Serialize(writer, cat);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
Response.Redirect("https://site1.com.au");
}
now i need to retrieve(deserialize) these data from site1.com.i have the deserialization code.i need to know how can i transfer this object? and am i using serialization for the correct purpose? hope my explanation is clear enough to understand the issue.
There is some chance that you want to render data in a form on site-1 and automatically post the form to site-2 with client side script.

Access __VIEWSTATE & __EVENTVALIDATION in C#

In ASP.NET, is it possible to get the values of __VIEWSTATE and __EVENTVALIDATION hidden fields into a variable in C# (server side) in, let's say, overriding the Render method?
I have tried:
protected override void Render(HtmlTextWriter writer)
{
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string temp = stringBuilder.ToString();
}
This gives me the entire ASP.NET output. We can get the values by using a string function, but I did not find it a very clean solution. Is there a better way to do this?
What I actually want is the values of __VIEWSTATE & __EVENTVALIDATION when the first request is made and not after the postback is done. That is when the output stream if formed when the first request is made.
If you look at the Page class using Reflector, you'll see these hidden fields are created during the render phase (look at the methods RenderViewStateFields and EndFormRenderHiddenFields).
You could probably get at some/all of the data using reflection (e.g. the internal property Page.ClientState).
But I don't think there is a clean solution (though to be honest I don't really understand what you're trying to achieve).
To get the event validation you should use HTML Agility Pack.
var eventValidation = HapHelper.GetAttributeValue(htmlDocPreservation, "__EVENTVALIDATION", "value");
public static string GetAttributeValue(HtmlDocument doc, string inputName, string attrName)
{
string result = string.Empty;
var node = doc.DocumentNode.SelectSingleNode("//input[#name='" + inputName + "']");
if (node != null)
{
result = node.Attributes[attrName].Value;
}
return result;
}

Getting the content of an ASP.NET System.Web.UI.WebControls.PlaceHolder

I have a server control that has a PlaceHolder that is an InnerProperty. In the class when rendering I need to get the text / HTML content that is supposed to be in the PlaceHolder. Here is a sample of what the front end code looks like:
<tagPrefix:TagName runat="server">
<PlaceHolderName>
Here is some sample text!
</PlaceHolderName>
</tagPrefix:TagName>
This all works fine except I do not know how to retrieve the content. I do not see any render methods exposed by the PlaceHolder class. Here is the code for the server control.
public class TagName : CompositeControl
{
[TemplateContainer(typeof(PlaceHolder))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public PlaceHolder PlaceHolderName { get; set; }
protected override void RenderContents(HtmlTextWriter writer)
{
// i want to retrieve the contents of the place holder here to
// send the output of the custom control.
}
}
Any ideas? Thanks in advance.
I just found the solution. I did not see the render methods because of the context of how I was using the PlaceHolder object. Eg I was trying to use it as a value and assign it to a string like so:
string s = this.PlaceHolderName...
Because it was on the right hand side of the equals Intellisense did not show me the render methods. Here is how you render out a PlaceHolder using and HtmlTextWriter:
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.PlaceHolderName.RenderControl(htw);
string s = sw.ToString();
Posting this as a second answer so I can use code formatting. Here is an updated method that uses Generics and also uses the 'using' feature to automatically dispose the text / html writers.
private static string RenderControl<T>(T c) where T : Control, new()
{
// get the text for the control
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
c.RenderControl(htw);
return sw.ToString();
}
}

Is there a way to create and render asp controls to a html string in the code-behind?

How would you create a new DropDownList (or any asp server control) and then render the html to a string in C#?
System.Web.UI.Control has a RenderControl(HtmlTextWriter) method that you can use to get the rendered content of the control as a string:
using(var sw = new System.IO.StringWriter()) // SW is a buffer into which the control is rendered
using(var writer = new HtmlTextWriter(sw))
{
myControl.RenderControl(writer);
return sw.ToString(); // This returns the generated HTML.
}

Categories