returning plain html from page method - asp.net

I would like for a webmethod to return a string of pure html.
The html isnt anything too fancy, a table and a couple form fields populated with values from a database.
Is their an easy way to build html within html? currently I am just dynamically building up a string, but it just feels dirty.
I could make a full blown aspx page, and then just strip out the bits I dont want from the resulting page leaving the plain html behind. but that also feels dirty.
Or there is the templating option (nvelocity etc) but thats seems like overkill.
any suggestion on something more efficient.
Tx

You can build a template file like xml to write the basic of your html with {0}{1}{2}{n} to put static content in it
Manipulate this template server side as you wish. You can add a list of data into this template.
Create a literal control on your page.
Put the server side created html in to you literal control. (myLiteral.Text)
You can create a basic asp.net file and add the html template in it by adding it from the literal control.

Related

Web Api/Knockout html encode post

I'm writing an application that accepts form posts for a knockout/jquery mobile application that later users can pull down the posted data and view what was filled in on the form.
A user could potentially put html or script tags in an input. Right now it appears that when the data is brought down and put back into the form any html tags or scripts are displayed in the inputs and everything is fine. Even labels that have html seem escaped correctly and do not actually display as html.
but how can I be sure that if a user inputs some html in a form input like
<h1>Show This</h1>
That when it is displayed in the form later it doesn't actually mess with the display of the form or run a script tag or something.
Does Web Api or Knockout provide any sort of automatic encoding where this is mostly covered to prevent this scenario?
The text binding in Knockout will escape the value by create a text node and populating it with the value.
The html binding, on the other hand, would not do this type of escaping and could potentially execute code, so you would not want to use it with user entered input.

Html in ASP.NET server control

Is there a way to write pure HTML when i'm developing my ServerControl in ASP.NET?
I want to create my control as .dll file. But when I'm writing my control I can only add HTML tags and attributes in C# in my .cs file.
And so i am forced to use this in my RenderContents() method:
output.RenderBeginTag(...
output.AddAttribute(...
output.RenderEndTag(...
instead of for example:
<div attribute1="value"></div>
is there a workaround of this problem?
Unfortunately only user controls (ASCX) allow for this type of approach (delcarative markup) but they cannot be packaged into DLLs. I've always hated this restriction, to me it's a code smell programmatically generating markup, but this is what we must live with!
If you have very static markup you could always put it into a resource file (resx) instead of outputting the HTML programmatically

What control to render dynamic HTML text on an aspx page

Page_Load generates a string of HTML for a dashboard. (html)
What control on an aspx page to bind that "text" to so when the page renders you see the tables, and buttons within?
Tried
With dhtml.Text = html but I don't see the buttons. I do see the tables as well as the borders of cells that I expect.
Any ideas?
TIA
You can inject any text/html into your ASPX page using: <% =GetMyText() %> where "GetMyText()" is a public or protected method in your code behind that returns a string.
You can also drop a Literal control onto a form and set the text via its "Text" property.
But if you want to do things the ASP.NET way, you might use a Gridview or Repeater to display tabular/repeating data, and Databind to it with some data.
If you are starting out with ASP.NET, you would probably be better off learning ASP.NET MVC as it is easier to get your head around if you are used to writing HTML. ASP.NET Web Forms, which you are using, generally tries to insulate you from HTML, CSS, and Javascript by giving you controls that you drop onto the page and bind data to. The controls do a lot of work for you, but take away almost all control of your HTML, CSS and Javascript.
I use javascript to dynamically create html elements. Your page_load function could register a javascript function which creates the elements you need.
Not sure why you were downvoted, but a very simple one to use is the HtmlGenericControl.
Basically, just add a span or div to your .aspx file and give it an ID and the runat="server" attribute.
Then, in your code behind just set the InnerHtml property of that control to your generated html.

How do I retrieve HTML dynamically generated from an aspx page?

To be specific, here is what I am doing, and here is what I am trying to do:
I'm coding an ASP.NET page, with VB code behind. When the user clicks a button on the page, I send them an email with information and instructions. Rather than sending a plain text email, I send a nice, pretty, HTML-formatted one. Right now, I'm doing this in a way that I KNOW will be difficult to maintain. That is, I'm straight up writing out all of the html. i.e.
markup += "<fieldset>"
markup += "<legend>"
markup += "Required Documents"
markup += "</legend>"
...and so on. Is there a way to create an aspx page (with vb code behind), and send the html of that page in the body of the email? The information is dynamic, so this pseudo-page would need logic in the on-load event to format the html correctly.
Thanks!
WebClient client = new WebClient ();
string html = client.DownloadString("http://domain.com/emailtemplate.aspx?id=1");
If you have access to a database you can always drop the html in there otherwise, I solved this problem by creating a mailtemplate.html file with [replace] sections in it so all you have to do is read the file into a string object do your replaces and then send it out. If you have to you can maintain multiple templates this way. I use it mostly as a wrapper on emails my systems need to send out so my template has a [body] tag in it that gets replaced with whatever message I need to send. I have also used this method to wrap multiple files into a single email output.
I assume you want to build the html on the fly... One (certainly the most maintainable) solution is to build a template based system.
Technically you maintain your html (e.g. email shots) in a directory read the templates from your ASP.NET program, fill in the details and send the html mail to the user.

Can you use an ASP.NET web control (ASCX) to render XML?

Off the top of my head, I don't see any reason why you couldn't structure an ASP.NET custom web control (ASCX file) to output only XML.
If you avoid using controls in the System.Web.UI namespace, and just use XML tags and sub-controls, which also render only XML, you should end up with a perfectly valid XML document being rendered.
Am I right, or is there something I'm missing?
If you want to generate an XML document and return that to the client, then it's probably easier if you create a http handler (IHttpHandler) instead of using a page and user controls.
You are missing that the control has to be within a page which would have some HTML output for a tag marking the start and finish of the control though there may be a way to override that.
Your idea could work out if your ASCX's generate pure XML, and you overwrite the Page's Render methods to replace the HTML tags that form HTML, BODY and FORM etc. elements.
The question is what you would gain with this approach over simply generating an XML document and building a class library which generates those XML fragments you wanted to generate using the ASCX's.

Resources