Programmatically writing CSS in .NET - c#

We're displaying objects within a web-page which have basic height and width properties. We can generate the HTML objects utilising the HtmlTextWriter object and plan to apply the Height and Width properties by referencing the object's ID in embedded css class at the top of the class.
#bob
{
height: 20
width: 20
}
...
<div id="bob"></div>
1) Firstly, is this a good approach or should we be considering another approach like inline css ?
2) There doesn't appear to be anything written in the surrort of rendering css within the .net framework. Does anyone know of anything which might be of assistance in rendering css notation with the .net framework. Is there an equivalent to HtmlTextWriter ?
3) Do you know of any tools which can be of assistance in generating css from code ?
Kind Regards

Not sure if you can do it in .net. But I am doing it in jquery as below. I hope this is someway useful.
$.rule("#bob {height:"+ht+"}").appendTo('style');
When this is generated then you can check in your dom that some css has been added to style element.
It is better than inline css. It works for the webpage which is displayed on the browser and not physically written to the file.
More info - here
It works in IE8, FF and Chrome when I checked.

Why are you writing out HTML, does one of the existing controls not provide what you need?
In general, CSS should be in a .css file.
It's not really possible to answer if this is a good approach since you haven't explained the intent of the code.

Try almost not to use inline css and you can use a literal that has the css string in it, and let the htmlTextWriter write it.

Related

Change default style of all asp:HyperLink tags

He, I'm currently restyling a site that I'm working on. It's a VS2013 project and I'm still getting the hang of using Visual Studio for web dev.
Currently the project has quite a lot of <asp:HyperLink></asp:HyperLink> tags
I know that I can use <asp:HyperLink CssClass="testingCss"></asp:HyperLink> to change the css of these HyperLinks in css using classes but there are so many links that I'd have to edit to fix this. Is there any quicker way of dealing with this? Like making a default text color property in my site's css file?
For example, the css used in p tags:
p {
color:red;
}
I'm sure there has to be a way to do what I want to do like this p tag example I gave. Would appreciate any help I can get here as it should save a lot of time and maintenance.
Thank you in advance!
It will be a <asp:HyperLink> in your markup but when the page is rendered, this will be output as a regular HTML anchor - <a>
For this reason you can target it the same way you would any other anchor:
a { color:red }
Just Add this code to your css
a{color:#ff0000}
Turns out my problem was the bootstrap css conflicting with my other css file. I just changed the css in the bootstrap file to get what I wanted. You guys were right to say that you just need to change the a tag for HyperLinks in asp.net projects. Thanks for the help.

Printing html file with pagebreaks

I am trying to create a program / service that can read an html file and print it.
I need to include page breaks; but don't know how to define them or make them print correctly.
The html files are mine, so I can add any elements to them to represent the page break position. I was thinking a hidden field, or using the page-break-before:always css style in the next element.
How should I approach this?
Css is the way to go. I'd recommend to create a class "page-break":
.page-break { page-break-before: always; }
Whereever you add this class to an HTML-element you get a page-break before this element (e.g. before every h1).
This tutorial covers almost every part of CSS and printing:
http://coding.smashingmagazine.com/2011/11/24/how-to-set-up-a-print-style-sheet/
hope this helps

How to remove wordwrap from a webbrowser and or set font size smaller

I am using a 2 web browsers to compare texts, and when the lines become to long it wraps my text, i would like to remove this or lower my font size to avoid this.
Can some one please advise me on how to remove the word warp or change font size on web Browsers?
The word wrap below totally miss alines my compare:
As far as I know, there is no property of the web browser control that can do this. The presentation of the web page is controlled entirely by the web page itself.
If you cannot change the web page itself, the best you can do is to possibly cache a local version and change its styling using WebBrowser.Document.ExecCommand(). WebBrowser.Document.InvokeScript() also works but requires already-defined JS, so we have to go ahead and add in the script manually.
HtmlElement head = webBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement script = webBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)script.DomElement;
element.text = "function adjust { document.getElementById('yourIdHere').style.whiteSpace='nowrap'; }";
head.AppendChild(script);
webBrowser.Document.InvokeScript("adjust");
Just find the div id of the text by looking at the web page's source code and you should be golden. If it doesn't have a div id, you can use other JS methods (such as getElementsByTagName) to find it.
You can not adjust font size or wrapper text from the web control its self. So you should adjust the styling of the elements inside the web browser.
I adjusted the styling at the same place i styled the green and red highlighting.
html.Append("<ins style=\"white-space:nowrap; display:inline; background:#e6ffe6;\">")
.Append(text)
.Append("</ins>");
"white-space:nowrap; display:inline;
The above code is what i added to remove word wrapping.
Inspired by Pomster's Answer, that's what I did to get the best result:
String.Format("<div style='white-space:nowrap; display:inline;'>{0}</div>",text);
c# 6.0 version:
$"<div style='white-space:nowrap; display:inline;'>{text}</div>";

ASP.NET Which HTML editor can do everything I want?

I have tried to use the standard AJAX HTMLeditor from here (http://www.asp.net/ajaxlibrary/act.ashx) and I have try to work with the FCKEditor (from http://ckeditor.com/)
But both don't do everything. I call the AJAX standard control A and the FCKeditor F.
With the A editor it is impossible to get your HTML text in the HTML content. You can only get it in the Design content. (this next code doesn't do the job: string htmlContentStr = Editor1.Content).
With F it is possible to get it in the HTML content (it does this by default), but to get your changes back in HTML is impossible. (this next code doesn't do the job: string htmlContentStr = FCKeditor1.Value).
So what I need is a HTML editor that is possible to put HTML text in HTML content, a user can make changes in the designcontent and after the changes 're make it must be possible to get the HTMLcontent and put it away in a string or database.
Is this possible or do I need a commercial one to get this feature?
If my question isn't clear, please let me know.
Thnx
I've used XStandard quite easily and it let me manipulate the HTML. I didn't bother using it as a control, but just read and wrote (escaped) the HTML where needed into the asp output.

Easiest way to display XML on an ASP.NET page

I have some XML in an XmlDocument, and I want to display it on an ASP.NET page. (The XML should be in a control; the page will have other content.) Right now, we're using the Xml control for that. Trouble is, the XML displays with no indentation. Ugly.
It appears that I'm supposed to create an XSLT for it, but that seems kind of boring. I'd rather just throw it into a control and have it automagically parse the XML and indent correctly. Is there an easy way to do that?
You could try to use XmlWriter/XmlTextWriter, set the writer's Indentation property, write to a StringBuilder or MemoryStream, and output the result inside a <pre> tag
A quick (and dirty) way of doing this would be to use an IFrame.
In truth, an XSLT is the "ideal" way for formatting an XML for display. Another option would be to parse it manually for display.
To use an Iframe:
ASPX side:
< iframe runat="server" id="myXMLFrame" src="~/MyXmlFile.xml" /></pre>
Code Side:
myXMLFrame.src = Page.ResolveClientUrl("~/MyXmlFile.xml")
You can find a slightly modified version of the XSLT that IE uses to transform XML to HTML when viewing in IE at http://www.dpawson.co.uk/xsl/sect4/N10301.html#d15977e117.
I have used it in a WebBrowser control in a WinForms application, and it works lika a charm. I have not tested it in FireFox/Chrome/Safari/Operat, though.

Categories