ASP.NET Adding ContentPlaceHolder to MasterPage - c#

I'm building a kind of simple In-Application CMS and I'm gonno let the user to upload an HTML and a CMS file as Masterpage.
A solution I thought was asking user to put some Pre-Defined Tags inside HTML so I can replace them with ContentPlaceHolder control. The reason of replacing ContentPlaceHolder is that I may have some Web Controls and need postback handling so I cannot convert everything to Html and put them to an HTML and push to client.
And the Questions:
1- Is there any better solution?
2- Is it possible? How can I replace some string with CPH Control?
Regards

To my knowledge you cannot dynamically generate content placeholders at runtime. So your actual masterpage will have to have them in place ahead of time.
I think you may be able to achieve what you are after by putting Literal controls before and after each content placeholder. Then you could parse your CMS html to determine which html comes before a placeholder and which comes after. Finally set each Literal's text property to the respective parsed html.

Related

Best way to give element custom properties in C# Webform

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

when text contains .css file showing in literal control affects the page style

I have a page where I am showing user input content in a literal control, The user input text may be html. and the html content contains an href which is a .css file, and when showing in page the style in .css file affect the whole page.
how can i solve the issue?
thanks,
Place the content in an iFrame which will render in its own DOM and then make the iFrame fit into your design somehow.
I think you can use Literal Mode property.
Mode = "Tranform"
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.mode%28v=vs.110%29.aspx
As per the link
Transform
Unsupported markup-language elements are removed from the contents of the control. If the Literal control is rendered on a browser that supports HTML or XHTML, the control's contents are not modified.

How to add html saved to a blob directly to page .aspx source

I have a page that I need to allow a user to edit, then I parse the information into HTML and save it to a MS SQL 2008 R2 database. I need to then add this information to an announcements page from the items contained in the database.
I am using C#, so the question is how would I do this? I have a div specifically for the content. Also, is this the best way to allow a user to manage content if I cannot use a cms ( this question is not so vital as I know it is prob more complicated than I realize)?
You can use an asp:Literal control to insert the HTML on the page via the "Text" property on the server-side:
Markup:
<asp:Literal ID="litAnnouncement" runat="server" />
Code-behind:
string htmlAnnouncement = GetHtmlFromDB(); // Get the HTML however you need to as a string.
litAnnouncement.Text = htmlAnnouncement;
I would put the code above somewhere in one of the Load or Init events, either for the page or the Literal control. Of course, there are other ways to do this, but I think this is the most straight-forward given your description.
I didn't get your question completely. but if you want to save data from an HTML element into the SQL data base, then you can add a Sqldatasource control to your page, and then define it with a parameter in the source code.

Can I put HTML code in label to display in my page?

ASP.NET with C#:
I have a web page which is intended to be a help page. Actually I am using a treeview component, and when someone clicks on a node a label on the page will change text by reading it from text file.
what I want is to put some images in the help so a .txt file will not be suitable. So I may use a .html file instead.
How to read such a .html file and display it in my page? May I replace the label with some thing else or what?
Use ASP.NET Literal control instead. The Literal control is used to display text on a page. The text is programmable.
Note: This control does not let you apply styles to its content!
<asp:Literal />
Use <asp:PlaceHolder runat="server"> - it's a more general purpose way of inserting blocks of content into a page.
Use placeholder or literal asp control, because these controls don't put any html for its self, it just render whats inside.
But for example the normal asp label will render as a span.

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