MVC: C#: display content of a contentPlaceholder twice - c#

I'm writing an MVC C# application. I use a masterPage and have
the title and content of other pages put in contentPlaceholders, which are
displayed on the master page.
On the MasterPage, I want the TitleContent written in both the <title> tag
and in the <body> section (in a <h1> tag).
I'm not allowed to do this, because for some odd reason you're not allowed to
use the same contentPlaceholder twice on a page.
Until now, I've been using
(FindControl("TitleContent").Controls[0] as LiteralControl).Text
in the <h1> tag, which worked fine until I started adding dynamic content
to the TitleContent placeholder (eg C# code).
How do I display this content twice?

If the text you want to display is in ViewData or the Model your views are bound to, you maybe able to access it as many times as you want and set it on any element on your view/master page.

You can have model view class were you store necessary string. Then make you views as strongly typed with the type of the class mentioned above. Finally inside tags make
<title><%=Model.PageName%></title>
and
<h1> <%=Model.PageName%></h1>
Hope it helps.

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

Can I retrieve an item that was added via the Page Editor in the codebehind of my page? (Sitecore, C#)

I'm new to Sitecore and in the codebehind of my page, I'm setting values for Google analytics based on the information that I have for a previously designed page (I can't change the basic structure of these pages). Most of the fields I need are set in Sitecore in the Content Editor, so I can access them via
Sitecore.Content.Item.Fields["fieldname"]
However, one thing I need is the URL of the logo image on each page, which is inserted with the Page Editor. Is it possible for me to get Page Editor objects on the page in the codebehind?
Google Analytics is client-side; consider using client-side script to retrieve the value of the logo URL. To do that, your script will need to execute after the page has loaded.
If you're using jQuery, you could do something like this
jQuery(document).ready(){
var logoUrl = jQuery('.logo').attr("src");
}
I'm assuming by Page Editor you mean the Design view. If so, click the Source button to view the markup and find the control that contains the URL. Give that control a unique ID and make sure it has the runat="server" attribute. After that, you will be able to access that control in the code behind by using Me.<uniqueID>.
No, Page Editor (Experience Editor) works with JavaScript to change anything on page. Then changes that were made on page are transferred to server and applied there, but it is not relate to codebehind of your page at all. It is all is done by custom Sitecore pipelines. However after applying changes(Save button) you'll be able to access to changed field via your codebehind on page reload.
So, you have two options:
Patch Sitecore "SaveUI" pipeline and add processor to it to get changes, and do something with them.
Wait after changes are applied, page will be reloaded and you will be able to access changed field in your codebehind

ASP.NET Adding ContentPlaceHolder to MasterPage

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.

Add server controls to a page that have no form

I have A single page that has the following structure
<form runat="server"><placeholder></placeholder></form>
I have a control call 'feed' that gets added to the placeholder. The control that gets added, holds multiple other controls called 'product'.
I load the products into the feed dynamically with ajax.
The problem is, that the mark-up in the product control contains server side buttons that require a form tag with runat=server. Otherwise the page wont compile.
The page compiles fine once I add the necessary form tags to the 'product' control. This is not satisfactory as I don't want multiple form tags on the page at once.
Is there any way around this?
You can either add a single <form runat=server> that wraps all server controls or don't use server controls (normal html tags).
Or, use a more flexible framework like MVC that gives you full control of your markup.

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.

Categories