Add server controls to a page that have no form - c#

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.

Related

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

ImageButton events on Nested Masterpage in dynamic usercontrol not firing

I have found many equivalent questions here, but non seem to apply to my problem.
I am dynamically loading controls into a Nested Masterpage. The button click event is not firing for buttons in different usercontrols, loaded in the Nested Masterpage.
When debugging, it seems like just a normal postback that occurs when a any button is clicked on the nested masterpage. No events are fired / caught in debug. The nested masterpage re-loads (post back), and that is about it.
Some Info that might pertain to the situation:
I am using codebehind for my methods, and where I expect to catch the event.
I have put the code that loads the uc's in the Nested MasterPage's init sub.
I have tested, and adding the control to the panel manually, everything is wired up as expected, throughout the lifecycle, and events are fired for the manually added control.
I have tested with using a static client ID for my buttons, as I thought the event might be wired up incorrectly. This did not have any effect.
I have manually assigned unique id's to my controls, no effect.
All usercontrol have and keep the correct properties set for them (Description, Quantity etc.).
I am reloading the usercontrols every time the nested masterpage loads.
I am not exiting on ifIsPostback, but instead reloading controls on every postback, as one of the buttons on the controls, might remove that control, and the new remaining controls need to be rebuilt on Page Init of the masterpage.
There is a script manager on the Master Page (not the nested masterpage).
I am making use of Ajax in the content pages, but not in the Nester Masterpage.
There is no update panel on the masterpage or nested masterpage.
All events are firing in a normal fashion on the content pages.
From my content pages, I remove and add controls to the nested masterpage dynamically. This works well and controls are loaded accordingly.
I am not setting the controls from the content page to the masterpage directly.
I am creating controls in my content pages, and pass them to a routine that manages a custom object containing all my controls, add and removing controls form this object as necessary.
In my Nested Masterpage Init event, I step through my custom controls
object and add them to a panel.
I am not sure how to debug the postback event in the browser, as I am
not making use of an udpate panel and Ajax for these controls in the
Nested Masterpage.
Here is the generated HTML for the Control that works (Manually dropped in at design time)
<input type="image" src="bookings_media/buttons/add-another-unit.jpg"
id="ContentPlaceHolder1_ucAddAnotherUnit1_butAddAnotherUnit"
name="ctl00$ctl00$ContentPlaceHolder1$ucAddAnotherUnit1$butAddAnotherUnit">
This is the rendered HTML for the dynamically added control:
<input type="image" src="bookings_media/buttons/add-another-unit.jpg"
id="ContentPlaceHolder1_ctl02_butAddAnotherUnit"
name="ctl00$ctl00$ContentPlaceHolder1$ctl02$butAddAnotherUnit">
Any ideas what might be snagging here:
Try using OnCommand instead of OnClick regarding the ImageButton.

Iterating over Sitecore Placeholder

I've been trying to iterate over a placeholder in sitecore. Essentially, there control that needs to be repeated by the page for a collection of elements (say a tab). I've only gotten the placeholder to render once. The following tabs don't have content inserted into them.
The code for something like what I'm trying to do is:
<asp:Repeater ID="rptTabs" runat="server">
<sc:Placeholder ID="plSocialSharing" runat="server" Key="Social"/>
<sc:Placeholder ID="plTab" runat="server" Key="content"/>
</asp:Repeater>
Should something like what I'm doing work? If it doesn't, do I need to user another sitecore control (something more dynamic?). Should I instead be using user controls I place there, or should I stick with the sitecore framework approach?
Since each of your tabs will contain the same rendering the I would not bother with placeholders. I think you will be adding more complexity than is required.
Assuming you are going to be using the jQuery UI Tab plugin then I would use the same technique you used in the previous question you asked, i.e. render out the content of the div tabs in the repeater, and you will need another repeater to create the ul list of the actual tabs.
Assuming you have a tree structure like:
- Social Sharing
-- Facebook
-- Twitter
-- Email
You could now Social Sharing use as the datasource of your Repeater and still allow the content of the tabs to be editable if you use Sitecore controls.
If you wanted something much more dynamic, like different rendering for each of the tabs, for example one with rich text, one with 2 column, one with table etc, then again there are a couple of ways of achieving this. One way would be to use standard <asp:PlaceHolder> in your repeater and add the rendering in this from your codebehind on ItemDataBound event.
Another option would be to add a bunch of different renderings into the placeholder in the page editor and set the datasource of each to content item. It will be difficult to use jQueryUI Tabs with this though since you would want markup like this in each control to make them self contained:
<div class="tab-title">Tab Title</div>
<div class="tab-content">Put whatever content you want in here</div>
As long as each rendering followed this structure then it would be easy to add several of these to the page and they would still be editable in the Page Editor, albeit listed one after another (not in tab format) in Editing mode. You would need to roll your own tab plugin, but it could be something as simple as:
Only in Preview or Normal mode
Get all .tab-title elements
Create a ul list and prepend to the tab container
Now call jQuery UI Tabs on the element
Hopefully given you some options at least, I can expand on any of these if it something you need but will have to get some code samples together.

Loading & Retrieving data from multiple user controls

I have multiple user controls on the page that are used primarily for data entry purposes. When a product is loaded, I need to load the product data into all those user controls and retrieve data when I need to save the product. The user controls are not visible to the user directly, instead user would click on a link and the user control will open up in a modal popup.
Currently the way I'm doing this is, I've loaded all the user controls on the page in separate div controls, and showing the modal popup when the link is clicked. I'm sure loading all the user controls on the page is not a good idea. Is there a better way to handle this? I think we can show the markup using JSON with jQuery - but how can I load and retrieve the data using that? Can someone help please?
Thanks.
Sounds like a case for AJAX. You could use an UpdatePanel within the div that defines the modal dialog. From the click handler for whatever control brings up a particular modal, you can dynamically replace one usercontrol with another in that UpdatePanel, pre-populate the usercontrol's data, then show a single modal that could be anything. Doesn't have to be a modal either; you could set up the UI with tab-like controls that switch between these UserControls in an always-visible div.
For a simpler approach, you could keep your Usercontrols in a div that is initially hidden by JQuery.
On postback, ie. when the user clicks the button/link for a particular product you can populate all your controls in a standard ASP.Net way so they're ready to display and then call a bit of JS/JQuery to show the div as a modal dialog.

MVC: C#: display content of a contentPlaceholder twice

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.

Categories