ASP.NET Parsing raw HTML into Controls - asp.net

Is it possible in ASP.NET to take a string containing some HTML and make ASP.NET to parse it and create a Control for me? For example:
string rawHTML = "<table><td><td>Cell</td></tr></table>";
HTMLTable table = MagicClass.ParseTable(rawHTML);
I know that this is a bad thing to do but I am in the unfortunate situation that this is really the only way I can achieve what I need (as I cannot modify this particular coworker's code).
Also, I know that LiteralControl allows you to have a control with arbitrary HTML in it, but unfortunately I need to have them converted to proper control.
Unfortunately, HTMLTable does not support the InnerHTML property. I need to preserve the HTML tree exactly as it is, so I cannot put it into a <div> tag.
Thanks.

The closest I think you'll get is Page.ParseControl, which is the ASP.NET parser. The downside is that the text you have is a LiteralControl, since it doesn't have runat="server" on it - so you 'll do a very tiny bit of string manipulation beforehand.
In otherwords:
this.ParseControl("<table><tr><td>Cell</td></tr></table>")
will produce:
Control
LiteralControl
whereas:
this.ParseControl("<table runat=\"server\"><tr><td>Cell</td></tr></table>")
will produce:
Control
HtmlTable
HtmlTableRow
HtmlTableCell
LiteralControl

You could traverse the HTML string a token at a time (token defined here as HTML Element or HTML InnerText), and determine which control needs to be instantiated, and what attributes it needs. But, that would be something highly... evil in the writing.
Why exactly do you need it to be a "proper" control as opposed to a text inside of a Literal control?

Related

How to set a custom attribute to asp.net literal and read it in code behind?

How to set a custom attribute to asp.net literal and read it in code behind? There's no Attributes collection.
A Literal isn't a HTML element, it literally is a literal (sorry for that). Its content becomes verbatim output in the response so there are no attributes to pull.
Attributes on ASP.NET controls are properties of the control class so you could try descending from Literal and defining your own property.
Just because some may map to HTML attributes is purely chance as they're only relevant to the server when rendering the page usually, the control dictates what output actually happens and how the properties map to the attributes.
If you want a generic HTML element you can try HtmlGenericControl which should offer the basic HTML properties for you to play with.

returning plain html from page method

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.

Using the ASP.NET ClientServerManager method or property to get name of control

Does the ASP.NET ClientServerManager provide some method or property to return the name of an ASP.NET control in the generated html page that could be used to write the javascript (using RegisterClientScriptBlock) in the code-behind? The actual generated control names can be quite long and unknown (I am also using master pages). I would like a generic way to write the javascript text and have the actual names of the controls be added to the javascript string. I would expect some method that I pass in the name of the control and it returns the actual html control name. I have searched in the documentation of the ClientServerManager and could not find anything.
Control.ClientID is rendered as html tag ID, so you can use that property.
If you are worried about control names, check out ClientIdMode where you can specify this and make it much simpler.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Would this help your issue?

Rendering of asp.net controls

I find it hard when using asp.net controls, to make proper css files because I don't know what the html output for the different controls end up as.
For example, if I want to add a div tag using a asp.net control, it's not easy to know what kind of control I can use.
Are there any documentation that shows for each asp.net control, what the rendered html for that control will be? I understand that some controls will probably change it's output due to how it's configured, but most controls will at least follow a pattern here.
The best would of course be a service on the web where you can put in the asp.net control definition and get the rendered html out.
Currently I have to put a control into my webform, run it and check the source in the browser, and if it's not the correct html tag, try another control and repeat. That get's tedious quite fast.
If you want to know to what html-controls a server-control is rendered, you could call RenderControl:
Dim myGridView as new GridView
Dim sb as New StringBuilder()
Dim sw as New IO.StringWriter(sb)
Dim textWriter as New HtmlTextWriter(sw)
myGridView.RenderControl(textWriter)
' now we can have a look what asp.net has rendered: '
Dim gridViewHTML as String = sb.ToString()
The rendered html will even differ from browser to browser for example when ASP.Net thinks the client uses a "lower"-browser(BrowserCaps), a Panel will be rendered as Table instead of a DIV.
By the way, if you're testing my above code on controls inside of your page, you have to override VerifyRenderingInServerForm otherwise you get a "...must be placed inside a form tag with runat=server"-error:
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
Return
End Sub
I would recommend adding a CssClass to your WebControls, and doing all your styling using classes, rather than HTML element types. As Tim Schmelter says, the html can render differently for different clients (I seem to remember a Panel can be a span as well under certain circumstances).
To avoid actually having to add the CssClass each time, you can subclass the WebControl you want, then set it's CssClass in Control_Init.

Assign database value to hyperlink but not a runat=server

Its simple if I have an ASP.net page with an ASP.net linkbutton / hyperlink
and I obtain a value from say a SQL Database and I store it in the label...
For example:
this.myLabel.Text = someValueReturnedFromADatabase
This is simple because it goes right to the code behind page and set the text value
to the value returned from my database (aside from going into more details with data access layer, etc).
What I was wondering is what if I dont want to use an ASP.net linkbutton and I simply want to use an HTML link button (as I need to call the jquery fade function). How would I set the value someValueReturnedFromADatabase to a control that is not runat=server?
Have a variable in your codebehind:
protected string TextForLabel
Set it in Page_Load, or wherever:
TextForLabel = someValueReturnedFromADatabase;
Reference it with pointy-bracket percent notation:
<% =TextForLabel %>
You can set runat="server" on standard html controls. I do it all the time. Then you will be able to access their properties in your code behind just like you do for asp controls.
If I am not mistaken, for labels you can use .InnerText or .InnerHTML to change the text.
From what I know of, you need to make some sort of relation between your HTML document and the code behind to interact with data from a SQL database. Either that or you'll have to make the entire database connection etc. in the HTML header using script type="text/javascript" and script type="text/C#" or whatever language you use to develop.

Resources