Best way to give element custom properties in C# Webform - c#

I have a generic html element like this
<span v-bind:class="{ available: days.timeOne }" data-time="10:00" data-date="{{ days.date }}" class="home__visit-featured-days-item-buttons-time">10:00</span>
Which when it is being rendered, is having the vuejs tags being stripped.
I have encountered this issue before when using basic html elements and even control tags like and my solution was to add them manually in the code behind. I don't like this method as not only is long and tedious, it ties back end logic to the view.
Is there a attribute similar to ClientIDMode that I can use to stop these tags being stripped?

ASP.NET webforms will strip out attributes for server controls (those with runat="server") when attributes contain colon (:) characters because these attributes cannot translate to class properties in the back end. However, non-server controls (i.e. raw markup) should just render as written into the ascx file.
Your example doesn't have a runat="server" attribute so I would expect it to render as written. If, however, it is a server control, could you just use raw markup instead?
If it must be a server control I think your only option is to add your attribute in the code behind as you mention e.g. myControl.Attributes.Add("v-bind:class", "{ available: days.timeOne }");

I suppose you are using the CK Editor for entering the HTML code. I wouldn't recommend that since it's WYSIWYG and not a code editor and does such things as stripping some part of the source. If you can, please move your code to Static text web part or to the layout directly. If you need to have it inside the editable region area, you can specify protected source for the CK Editor to let it know what code not to touch:
https://www.google.com/search?q=ckeditor%20protectedsource&rct=j

Related

Get HTML Element Based on Style in C#

I have HTML in a string and need to use C# to extract a particular node based on its CSS style. In the past, I've parsed HTML using HtmlAgilityPack. This parses the HTML very well, and gives me all the elements in an organized fashion. And I can even filter or query by attributes. The problem is that it doesn't "understand" how the CSS hierarchy works. CSS can be inline, at the beginning of the file, or in a parent node. The browser is intelligent enough to know what the final rendering of an element should be. HtmlAgilityPack can give me the attributes of a particular element, but I don't see a way for it to tell me what the ultimate style of the element would be. For example, an element might not have any attributes, but yet have a particular style based on the more complex CSS logic (style from a parent node, etc.)
How can I query the in-memory HTML, without using a headless browser, to find elements whose final rendering (were they to be rendered in a browser) would have a particular style? (Similar to how jQuery does it, eg: $('[color="#0000ff"]')
I don't want a lot of complicated regexs or similiar.
Thanks,
CSS (Cascading Style Sheet) use for the butterfly layout web application it's a one client site. A web page multiple CSS examples External, Internal, Inline use as user needed. Every CSS works the same goals but it maintains order. the higher order is the Inline CSS then Internal CSS and then external CSS we get it external file. Every CSS selector is not the same example id, class, adjoin, parent, sibling and others selectors. read this article https://css-tricks.com/precedence-css-order-css-matters/.
jQuery selector and CSS Selector are the same but jquery is work different way every jQuery CSS contain in Inline CSS. from your ask $('[color="#0000ff"]') is an a attribute selector. this html example are <div color="#0000ff">content</div> but it's not seticesfy W3C. it's also an custom attribute of an html element the W3C are recomended for custom attribute prefix data- example <div data-color="#0000ff">content</div> you got it http://html5doctor.com/html5-custom-data-attributes/.
--Thanks --

How to remove all hidden elements?

I am looking for a way to strip out all elements in the HTML doc that are hidden.
I am able to remove elements that contain style display:none or visible:hidden but what about those that are hidden because of their CSS class? Is there a way to look at say a DIV with class "item-description" and know that the stylesheet for this class contains display:none?
There is no way of knowing this with HtmlAgilityPack since it doesn't take into account the styles, only the DOM.
If you want to really be able to handle the styles after having loaded your html you must
parse the css (following all #imports and other tricks)
apply each rules to each DOM node by using the selectors
apply the inheritance rules to see what is hidden or not depending on the parent hierarchy and rules
resolve consistency problems (what if a component is visible but not its parent? How would that translate into your final document?)
I hope you decided on the browser you want to emulate, since some css rules will apply to specific browsers.
I also hope you don't have javascript that touches the DOM on the page...
I'm sure I'm forgetting some more things... Don't go there! There are grues!
If you really want to get this result in C#, perhaps embedding a rendering engine and then querying it through javascript would be a better way to do it.

Use JavaScript / jQuery to access CSS properties

How might I iterate over / access the properties of a referenced CSS file itself?
Hear me out, this may sound strange: I am currently wrapping the Google Maps API into some C# code (it outputs JavaScript); the Google Maps API, for things such as the Polygon class, appears to let you specify certain things like 'stroke-color' by passing in the appropriate value, but I can find no method for setting the classname for Polygon, nor immediately discover one for Polygon. So, since our HTML / CSS guy wants to skin stuff, I figured I'd use jQuery (.CSS()) or something to parse the CSS file, grab a defined property from within, and set the value in the JavaScript code.
Normally I'd set the color (for example programmatically, like so:
html.AppendLine("fillColor: '" + string.Format("#{0:X2}{1:X2}{2:X2}", fillcolor.R, fillcolor.G, fillcolor.B) + "',");
But I am trying to change it to something like this:
html.AppendLine("fillColor: $(\"Poly\").css(\"fill-color\"),");
And grab the property from a class in a referenced CSS file, defined like:
.Poly
{
stroke-color: #FF00FF;
fill-color: #FF00FF;
}
jQuery seems to want a defined element to grab CSS properties from...it does not appear to be grabbing the values directly from the CSS file (I could be wrong). The above code, unfortunately, results in 'undefined.' This may be because I am rusty with JavaScript, / CSS or it may be that the way I am attempting to access things is not correct.
Has anyone any ideas how I might accomplish my ends here?
Accessing JavaScript files is a little quirky. It behaves differently in each browser - you may try something like this:
document.styleSheets[0].rules[0].selectorText
The first index iterates over every stylesheet in the document, the second over each rule in this sheet.
There are some ways to access css property from a css file. They're a bit complicated.
Access CSS file contents via JavaScript
How do you read CSS rule values with JavaScript?
My solution for this case.
First, you create an empty div, which apply the selected css
<div class="Poly" id="dumbPoly"></div>
Then you select the div, get the css value property
html.AppendLine("fillColor: $(\"#dumbPoly\").css(\"fill-color\"),")
=============================================================================
You can set the div invisible by jQuery
$('#dumbPoly').hide();
Or set the style of the div
<div class="Poly" id="dumbPoly" style="display: none;"></div>

Implementing paging in Sitecore content pages

I have a section on my website where I plan to add a lot of text-based content, and rather than display this all at once it would be nice if I could add paging on just these pages. If possible, I would like to put all of my content within one content item and have the paging work automatically, building a URL along the lines of http://example.org/articles/title?page=2 or similar.
I've stumbled across an article that mentions paging with Sitecore items and this seems rather close to what I require, although mine requires pagination on a single content item, rather than multiple items. Can someone help me adapt this article towards my needs (if it's on the right track of where I should be looking)?
Is it possible to do this with a Sitecore content item?
http://briancaos.wordpress.com/2010/09/10/create-a-google-style-paging-component-in-c/
I think you'd either want to create your own WebControl and define a custom Render() method that reads the query string to write out the correct information, or you could even do it all in a Sublayout (a user control ASCX file). I've done this before by adding in a custom tag in the Rich text editor via Sitecore (I think I used <hr class="page-break" />) then in my ASCX I'd look for that HTML tag and split the content into chunks from that. I think my solution also used jQuery for some of it but you could probably do it with C# too.
Edit:
You'd want to split the tasks up and have the "paged" content as well as a list of pages (like the article you referenced) so you can easily generate the page buttons. Both of these could be done in two separate repeaters.
You can split the text from a single field into the different pages using approach described here: Split html string to page. All you need to do after that - read the query string and display appropriate block.
If I understand you correctly you have an Item in Sitecore that has x number of text fields and you only want a subset of those displayed depending on input in the querystring ?
In it's simplest form you want a sublayout that handles that.
Basically I'd imagine you having fields called Text1, Text2, text3 etc.
This .ascx could then retrieve the data for fields the fields you'd want using the control and adding them.
Then you could use the code from the article to generate the paging links.
This should be simple enough, but I'd say it would be a better idea to have an item in sitecore and use it's children as the data you want viewed and paged.
It's nicer because if you start out with 5 "page" fields and suddenly want 10, your item will keep on growing, where children can be added without bloating the parent page. Plus the user could then order the children how he sees fit.
I hope this helps a bit.

Safe HTML in ASP.NET Controls

Im sure this is a common question...
I want the user to be able to enter and format a description.
Right now I have a multiline textbox that they can enter plain text into. It would be nice if they could do a little html formatting. Is this something I am going to have to handle? Parse out the input and only validate if there are "safe" tags like <ul><li><b> etc?
I am saving this description in an SQL db. In order to display this HTML properly do I need to use a literal on the page and just dump it in the proper area or is there a better control for what I am doing?
Also, is there a free control like the one on SO for user input/minor editing?
Have a look at the AntiXSS library. The current release (3.1) has a method called GetSafeHtmlFragment, which can be used to do the kind of parsing you're talking about.
A Literal is probably the correct control for outputting this HTML, as the Literal just outputs what's put into it and lets the browser render any HTML. Labels will output all the markup including tags.
The AJax Control Toolkit has a text editor.
Also, is there a free control like the
one on SO for user input/minor
editing?
Stackoverflow uses the WMD control and markdown as explained here:
https://blog.stackoverflow.com/2008/09/what-was-stack-overflow-built-with/
You will need to check what tags are entered to avoid Cross side scripting attacks etc. You could use a regex to check that any tags are on a 'whitelist' you have and strip out any others.
You can check out this link for a list of rich text editors.
In addition to the other answers, you will need to set ValidateRequest="false" in the #Page directive of the page that contains the textbox. This turns off the standard ASP.NET validation that prevents HTML from being posted from a textbox. You should then use your own validation routine, such as the one #PhilPursglove mentions.

Categories