I am preloading stylesheets inside this method OnResultExecuting(ResultExecutingContext filterContext) in MVC, this is just to make sure that this is done before the actionResult is called when loading the page, and this works fine, my issue is now i want to modify the style sheet background-image property in a class called logo and before the tag is built(code below) and added to the head section in the DOM, I'm wondering if there isn't a way to read(StreamReader?/StreamWriter?) the css file and modify it and save it before the tag is built or something like that, btw Im getting the css file from the server.
foreach (string file in filepath)
{
if (Path.GetExtension(file).ToLower() == ".css")
{
TagBuilder link = new TagBuilder("link");
link.Attributes["rel"] = "stylesheet";
link.Attributes["type"] = "text/css";
link.Attributes["href"] = urlHelper.Content(
string.Format("~/Content/themes/Theme{0}/{1}",
setting.ThemeId, Path.GetFileName(file)));
resources.Add(MvcHtmlString.Create(link.ToString(TagRenderMode.SelfClosing)));
}
}
You could just load another style sheet after this one and amend class style that way, infact that's exactly how I do this for my white label solution.
CanĀ“t you use LESS to dinamicaly create your css?
http://www.dotlesscss.org/
http://karlmendes.com/2010/07/using-less-css-for-net-with-asp-net-mvc-2/
Maybe it will make things easier for you...
Related
I would like to get the font-size for an html element in a web page, for example a <p> tag.
This should be valid even for element without a style or class attribute, so it has to know dynamically inherited css attributes.
I've tried with html agilitypack, a very good lib, but of course it doesn't consider css rendering. Tried also with WPF webbrowser, but it seems it cannot get font size for every element, and for many of them it returns a percentage.
Javascript getComputedStyle() is not suitable since all should run server side.
Any ideas?
Try this:
Element.prototype.getStyle = function (prop) {
if (document.defaultView)
return document.defaultView.getComputedStyle(this, null)[prop];
else
return this.currentStyle[prop];
}
call:
document.getElementById("div1").getStyle("font-size");
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
I need some help figuring out how to do something.
I got this gallery (galleriffic) and some images that are store in Flicker.com so I used the flicker api to get the images but still add them manually to test the gallery.
Now I'm looking for a good way to insert the images into the html after I get them with the flicker api.
I found this htmltextwriter and used the function
Response.Write(GetDivElements());
but this is adding the div's on the top of the html and not inside the body tag.
my qustions is:
is HtmlTextWriter writer = new HtmlTextWriter(stringWriter) a good way to build html tags on the server side?
Is there a better way to add elements to the html other then Response.Write(""); ?
Here is what I do when I need to add mark-up.
in my page
<asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>
in my code behind
MyPlaceholder.Controls.Add(new Literal() { Text="<div>some markup</div>"});
I do it this way because:
1) you can put the PlaceHolder where you need it in the structure of your page
2) by adding a Literal at runtime to the Controls collection prevents ViewState getting bloated with it's contents.
If you are using the older style of asp.net, and not asp.net MVC, then you can just create a div with an id and runat="server". Then you can just write directly to the html.
aspx page
<div id = "DivINeedToAddStuffTo" runat="server" />
aspx.cs
DivINeedToAddStuffTo.InnerHtml = GetDivElements();
Also, I do not see anything wrong with using HtmlTextWriter to create your Html markup
You might try looking into Placeholders. That way you can create an instance of an image control and then add it your your placeholder.
Image myImg = new Image();
myImg.ImageUrl = "MyPicture.jpg";
myPlaceholder.Controls.Add(myImg);
You should be able to use the ASP literal control:
foreach (var item in items)
{
Literal literal = new Literal();
literal.text = item.html; //Assuming the item contains the html.
MyPlaceholder.Controls.Add(literal);
}
You could have that code before the page has rendered.
Hope that helps
Paul
EDIT
Sorry, I think I was mistaken, I thought you had the html with the link to the image(s) and not the actual image itself, Justin's answer would suit you if that's the case.
var ctrl = new WebControl(HtmlTextWriterTag.Div) { CssClass = "SomeClass" };
ctrl.Attributes["style"] = "float:left;display:inline-block;margin:3px;";
ctrl.Controls.Add(new Image
{
ImageUrl =
Page.ResolveUrl("image path here")
});
this.Controls.Add(ctrl);
I need something as a placeholder. I at first looked to Content Control as a solution but I'm having some problems with it.
I then looked into adding CustomXML to the .docx but turned away from that because of the i4i lawsuit.
Then I decided I would just plain change the text of the Content Control through OpenXML SDK 2.0 but even if it's so marked the Content Control doesn't go away. I guess that it doesn't know that the text changed unless it happens inside word.
I could perhaps just remove the CC and place text instead but I'm afraid of problems with format and styles it could bring and also it would kind of defy the purpose of the Content Control.
Then I started wondering if I could define my own placeholders that Word could recognize. Through Building blocks perhaps. It doesn't have to do anything except be easy to find using OpenXML and somehow taggable so I know what to replace it with. I'm not really sure what can be done with Building Blocks but I'm hoping it's do-able.
Not sure what solution would be best for me but what I need is:
a)Something that's easy to place in the template, perhaps predefined Content Control placeholders that you can place where you wan't and style as you like.
b)When the data has been added it removes all placeholders, it won't be modified again. It keeps the style/format defined in the placeholder.
TO RECAP, I need answer to either
How can I edit Content Controls in OpenXML SDK so they will be removed after text is added.
-OR-
Can I define my own custom OpenXML tag for a Word Document that I could then replace?
Perhaps this can help you,
private void DeleteSdtBlockAndKeepContent(MainDocumentPart mainDocumentPart, string sdtBlockTag)
{
List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
SdtBlock sdtA = null;
foreach (SdtBlock sdt in sdtList)
{
if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == sdtBlockTag)
{
sdtA = sdt;
break;
}
}
OpenXmlElement sdtc = sdtA.GetFirstChild<SdtContentBlock>();
OpenXmlElement parent = sdtA.Parent;
OpenXmlElementList elements = sdtc.ChildElements;
var mySdtc = new SdtContentBlock(sdtc.OuterXml);
foreach (OpenXmlElement elem in elements)
{
string text = parent.FirstChild.InnerText;
parent.Append((OpenXmlElement)elem.Clone());
}
sdtA.Remove();
}
Take a look at using a Field. The mail merge fields are designed for exactly this purpose.
I don't understand from your question if you are only interested in a solution that automatically removes the ContentControl/SDT when you modify it using the OpenXML SDK, or whether you want it to disappear after the content is modifed programmatically or by a user.
If the former, I think you'll have to remove it yourself as Bilel suggested. If the latter, you should look at this property: ContentControl.Temporary ("the ContentControl is automatically deleted when the user types in the control, or when the text in the control is changed programmatically. When the ContentControl is automatically deleted from the document, the text in the control remains in the document.")
i have setup a profanity filter with bad words in a XML file and have the following function to run on my page to replace the words:
BadWordFilter.Instance.GetCleanString(TextBox1.Text);
i'm about to go through my entire site now wrapping that function around every little text variable one by one and it's going to be a huge pain in the butt
i'm hoping there's a way that i could just set my masterpage to automatically run all text through this thing on any page_load, so that the effect would be site-wide instantly. is this possible?
much appreciated for any help
One quick tip I have is to use the tag mapping feature of asp.net for this:
Create a custom textbox class derived from the TextBox class
Override the get/set Text property & in the get part, return the cleaned string
Use tag mapping feature in the web.config file to replace all TextBox classes with your custom text box class & everything should work really well.
This link has a sample implementation which uses the HTMLEncode, but you get the idea: http://www.devwebpro.co.uk/devwebprouk-46-20071010ASPNETTagMapping.html
HTH.
I realize you said Page_Load(), but I suspect this will do what you need.
In the Page.PreRender event, walk through the controls:
/* inside Page_PreRender() handler...*/
if (user_options.filterBadWords == true)
{
FilterControls(this);
}
/* this does the real work*/
private void FilterControls(Control ctrl)
{
foreach (Control c in ctrl.Controls)
{
if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
TextBox t = (TextBox)c;
/* do your thing */
t.Text = BadWordsFilter(t.Text);
}
if (c.HasControls())
FilterControls(c);
}
}
This is a hack, which will get you through your current problem: overriding the TextBox control is ultimately a better solution.