I currently use SiteMapPath to generate a breadcrumb for my ASP.net 3.5 (vb.net) pages and it works great.
Now I am trying to figure out how I might be able to use the same control to build a dynamic page title (in the tag). I want a reverse path listed, but the SiteMapPath control includes links and bunch of styling spans. Is there any way to remove all of that, and just get a plain path with separators?
For example, Let's say we are on the "Press Releases" page inside of the "About" section of my site.
The breadcrumb shows up as:
Home > About > Press Releases
I want to have the page title be:
Press Releases - About - Company Name
So I need it to reverse the order, drop all spans, links and styling (since this is inside the tag) and drop the root node "Home" and then add the company name to the end. Since my menu nav and breadrumbs are all driven from the sitemap file, I thought it would make sense to try to make the title do the same.
Any thoughts? Thanks.
The best way to achieve your desired output is to ignore the SitePath control, and instead use the SiteMap's SiteMapNode's collection. The server parses the web.sitemap into a collection of SiteMapNodes and wires up the SiteMap.CurrentNode by finding a node that matches the current page's URL. Each SiteMapNode has a ParentNode property. Here is the reference page on MSDN.
So, all you need to do is check if the CurrentNode has a parent, if it does you add the ParentNode's title to the CurrentNode's title and keep going until you reach the RootNode (where you substitute your company name for the root node's title).
Below is a quick solution; it could go in the MasterPage if you are using one. I'm not sure your language, but this should be easy to rewrite in VB.Net. I gave it a simple test and it seemed to work.
You can customize the characters that separate the page titles.
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = SiteMapTitle(SiteMap.CurrentNode, "", " - ");
}
private string GetNodeTitle(SiteMapNode oNode)
{
if (oNode == SiteMap.RootNode)
return "Company Name";
else
return oNode.Title;
}
private string SiteMapTitle(SiteMapNode oNode, string szTitle, string szItemSeparator)
{
if (szTitle != string.Empty)
szTitle = szTitle + szItemSeparator + GetNodeTitle(oNode);
else
szTitle = GetNodeTitle(oNode);
if (oNode.ParentNode != null)
szTitle = SiteMapTitle(oNode.ParentNode, szTitle, szItemSeparator);
return szTitle;
}
Hope that helps...
Related
I have master layout and three sublayout header, content, footer. In header sublayout I have search textbox and search button, On click of search button result will display on content sublayout, On content sublayout I have repeater but I am not able to access repeater control in header sublayout.
I've dealt with similar problems in Sitecore sites before. There are two ways of doing this that I'd suggest you consider:
1) With some custom code, your Layout can act as an intermediary for the SubLayouts
Define some sort of "receive results" interface that can be implemented by any sublayout that wants to handle search results. For example:
interface IRenderSearchResults
{
void RenderResults(IEnumerable<SearchResultData> resultSet);
}
Make your "content" sublayout implement this interface, so that when something sends it search results, it can handle them:
public class ContentSublayout : Sublayout, IRenderSearchResults
{
public void RenderResults(IEnumerable<SearchResultData> resultSet)
{
repeaterControl.DataSource = resultSet;
// do whatever other data binding tasks are required
}
}
Then extend your Layout to have methods for "register me to receive search results" and "send search results to wherever they're needed":
public class MyLayout : Page
{
private List<IRenderSearchResults> resultControls = new List<IRenderSearchResults>();
public void RegisterSearchResultsControl(IRenderSearchResults control)
{
resultControls.Add(control);
}
public void DispatchSearchResults(IEnumerable<SearchResultData> resultSet)
{
foreach(var control in resultControls)
{
control.RenderResults(resultSet);
}
}
}
Your "content" control should then register itself when it's created, by calling the register method on the Layout. Add a method like:
protected void Page_Init(object sender, EventArgs e)
{
MyLayout layout = (MyLayout)this.Page;
layout.RegisterSearchResultsControl(this);
}
Finally, when your header control has generated the results to display, it should dispatch them with a call to the Layout:
public class MyHeader : SubLayout
{
public void DoTheSearch()
{
// whatever you do to perform a search
MyLayout layout = (MyLayout)this.Page;
layout.DispatchSearchResults(theSearchResults);
}
}
(This code was typed off the top of my head, rather than copy/pasted - so there may be typos - sorry)
That way any set of results receiving controls can exist anywhere on a page (and can be added / removed via Page Editor if you want) but whenever a search is run, if they're on the page they will get the results. This approach can also allow you to make things like the paging UI for the results, or a results summary display into separate controls that editors can move around the page.
Note that if you have multiple Layouts on your site, you probably need to make sure that the dispatching code is in a base class of all your Layouts, to make sure it's always available if an editor changes the layout of a particular page.
2) You could probably use Sitecore's event model
This article gives a fairly helpful description of how the internal framework for events works: https://adeneys.wordpress.com/2012/10/21/decoupling-through-the-sitecore-event-pool/ but there are others if you google.
The best way to do this would be to use the querystring to hold the search parameters, which is the standard tried and tested way to do this.
When the user presses the search button you then need to redirect to the current page with the quersytring parameters added. Then have the sublayout with repeater read the querystring parameters to list the results, rather than pass data from one sublayout to another.
The use of the querystring means that users can bookmark search results and removes the need for a complex event model.
Also by using the browser layout as an intermediary for passing data means you are tightly coupling the sublayout and the browserlayout which may not be good if you need to use the sublayout else where.
Summary
I'm having style issues when flipping master pages via a button event in asp.net 4.0. The new master switches, but the css from the old master remains. I don't understand how this could happen as the styles are defined within the head of the old master, and i can clearly see via the markup the new master is being displayed with whats supposed to be a totally different set of styles. Also, viewing source shows all the new css declarations in the head. How can i get this to "refresh" or "reload"?
Some details
I'm implementing a mobile version of my asp.net site. If a mobile device is detected i set a cookie and switch the master page in the preinit to a mobile friendly one. This works fine:
protected virtual void Page_PreInit(Object sender, EventArgs e)
{
if (IsMobile)
this.Page.MasterPageFile = "m-" + this.Page.MasterPageFile;
}
I have a "full site" button at the bottom that allows you to flip back and forth between the mobile and desktop view. When clicking it, i change the value in the cookie. Then when the page redirects to itself, the value is checked, and it gives the respective masterpage. This also "works", i can tell the right masterpage is rendering via markup. Except the styles from the mobile version remain even when the desktop master is being displayed. I did the redirect thinking it would prevent this.
// desktop/mobile site toggle button click event
protected void viewMobileButton_Click(Object sender, EventArgs e)
{
HttpCookie isMobileCookie = Cookies.snatchCookie("isMobile");
if (bool.Parse(isMobileCookie.Value))
Cookies.bakeCookie("isMobile", "false");
else
Cookies.bakeCookie("isMobile", "true");
Response.Redirect(Request.RawUrl);
}
This is the first time I've done anything like this, and not sure if i'm even going about it the right way, or how to debug from here. Thanks in advance for any help.
Edit
Ok, so i figured out it's related to the JQuery Mobile Scripts. JQuery Mobile has this way of tying pages together. I don't fully understand it, i think they use it for page transitions, and it's preventing my new CSS from registering. When i turn it off, my masterpage flips fine with css included. I'm looking into a way to turn off JQuery Mobile before my redirect. Note sure how though yet.
The problem ended up being related to JQuery Mobile AJAX for page-transitions. JQuery Mobile does not load the head of the document on additional page requests after the first.
So when i'd switch the mobile master to the desktop master, the head of the document wouldn't load to bring in my styles. There are a few way's this can be fixed:
This way just turns off AJAX altogether, and fixes the problem, but then you can't benefit from it:
<form data-ajax="false">
This is a way to do it problematically, but remind you, it will not work via an event after initialization of JQuery Mobile, so again you can't benefit from it:
$.mobile.ajaxEnabled = false;
The above two solutions i support could work if you redirected through a page first if you have to use an onclick event and an event handler.
A better solution is to add rel="external" to the link to tell JQM it's and outgoing link.
<a href="myself.com?mobile=true" rel="external" >
But because i couldn't run some code i wanted to in order to change the cookie, i had to pass a query string parameter, check it on the preinit, then set the cookie which my page also looks at on the preinit and flips the master.
Here's my full solution below in case someone is out there doing the exact same thing. Note because my website is using aliasing, i had to read Request.RawUrl and parse it myself since the Request.QueryString object did not contain the values i passed.
// reusable function that parses a string in standard query string format(foo=bar&dave=awesome) into a Dictionary collection of key/value pairs
// return the reference to the object, you have to assign it to a local un-instantiated name
// will accept a full url, or just a query string
protected Dictionary<string, string> parseQueryString(string url)
{
Dictionary<string, string> d = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(url))
{
// if the string is still a full url vs just the query string
if (url.Contains("?"))
{
string[] urlArray = url.Split('?');
url = urlArray[1]; // snip the non query string business away
}
string[] paramArray = url.Split('&');
foreach (string param in paramArray)
{
if (param.Contains("="))
{
int index = param.IndexOf('=');
d.Add(param.Substring(0, index), param.Substring(++index));
}
}
}
return d;
}
Then i just use my dictionary object to evaluate and rebuild my url with the opposite mobile value, dynamically setting the href on the toggle link. Some code is obviosuly left out, but for perspective, base._iPage.QueryStringParams hold my dictionary object that was returned, and base._iPage.IsMobile is just a bool property i also have via the page interface i use, that all my pages, and user controls, ect, can talk to.
// get the left side fo the url, without querystrings
StringBuilder url = new StringBuilder(Request.RawUrl.Split('?')[0]);
// build link to self, preserving query strings, except flipping mobile value
if (base._iPage.QueryStringParams.Count != 0)
{
if (base._iPage.QueryStringParams.ContainsKey("mobile"))
{
// set to opposite of current
base._iPage.QueryStringParams["mobile"] = (!base._iPage.IsMobile).ToString();
}
int count = 0;
url.Append('?');
// loop through query string params, and add them back on
foreach (KeyValuePair<string, string> item in base._iPage.QueryStringParams)
{
count++;
url.Append(item.Key + "=" + item.Value + (count == base._iPage.QueryStringParams.Count ? "" : "&" ));
}
}
// assign rebuild url to href of toggle link
viewMobileButton.HRef = url.ToString();
}
Then on my pageinit this is where i actually check, first the quesry string, then the cookie, if neither of those are present, i run my mobile detection method, and set a cookie, and my interface bool property for easy access to conditionals that depends on it.
QueryStringParams = base.parseQueryString(Request.RawUrl);
if (QueryStringParams.ContainsKey("mobile") ? QueryStringParams["mobile"].ToLower().Equals("true") : false)
{
Cookies.bakeCookie("isMobile", "true"); // create a cookie
IsMobile = true;
}
else if (QueryStringParams.ContainsKey("mobile") ? QueryStringParams["mobile"].ToLower().Equals("false") : false)
{
Cookies.bakeCookie("isMobile", "false"); // create a cookie
IsMobile = false;
}
else
{
IsMobile = base.mobileDetection();
}
if (IsMobile)
this.Page.MasterPageFile = "m-" + this.Page.MasterPageFile;
}
Is there any way in which I can search the attachments and show the attached documents in the search result? My search result should show only the attachments in which the search text is contained. Right now I can search the attachments and the page in which the attachment contains search text is shown.
Say, I have a Page Home and attachment myattachment.docx as its attachment. While searching Background as search text in the Site search, which is contained only in the myattachment.docx (not contained in Home page) , the search result show Home page as a search result. What I am intending is to return something like Home/ myattachment.docx as a result instead of Home page. My page may any number of attachments.
Thanks In Advance!
The info on this is a little bit sketch. See below for the documentation I used.
Create a custom global event handler in AppCode (or Old_App_Code), make sure it is a partial class of CMSModule Loader.
Add your custom event handler in the overriden Init() The one you want is DocumentEvents.GetContent.Execute.
The sender object should be the current TreeNode being indexed for search. You can then use that node to access the relevant attachments and modify the event args e.content to add your document text to the search.
[CustomDocumentEvents]
public partial class CMSModuleLoader
{
private class CustomDocumentEventsAttribute : CMSLoaderAttribute
{
public override void Init()
{
// Assigns custom handlers to the appropriate events
DocumentEvents.GetContent.Execute += Document_GetContent;
}
private void Document_GetContent(object sender, DocumentEventArgs e)
{
TreeNode node = sender as TreeNode;
if (node != null)
{
//Note, this is psuedo code, this isnt the way to iterate over TreeNode.Attachments
foreach( attachment in node.Attachments ) {
e.Content += attachment.content;
}
}
}
}
}
More info
See http://devnet.kentico.com/docs/devguide/index.html?event_handlers_overview.htm for implementing custom events in general in version 7.
See this for custom search in version 5
http://devnet.kentico.com/Knowledge-Base/Search/How-to-search-for-documents-using-assigned-categor.aspx
See http://devnet.kentico.com/Knowledge-Base/API-and-Internals/Custom-Handler-Library-compatibility.aspx for the updated event name for version 7 referred to in the version 5 custom search example.
I'm following this example: http://msdn.microsoft.com/en-us/library/ms178210.aspx
And I can get it to work with just a single page and a code behind, but when I add a masterpage, the examples doesn't work properly. Within my master page, I have a head content section and a body content section. It's nothing fancy.
How do i do client callbacks with master pages?
A more scalable approach would be to use the following syntax (replace the ResultsSpan with an aspx Panel too)...
function LookUpStock()
{
var lb = document.getElementById('<%=ListBox1.ClientID%>');
var product = lb.options[lb.selectedIndex].text;
CallServer(product, "");
}
function ReceiveServerData(rValue)
{
document.getElementById('<%=ResultsSpan.ClientID%>').innerHTML = rValue;
}
This way, if the name (or actual page) of the MasterPage changes the code will still work.
Basically the ASP.NET process parses the page and replaces the <%=%> directives with the correct name of the control on the client.
This approach will also work if you have nested controls. In your example, if you had a control nested inside another panel the rendered id could look something like MASTERPAGEPREFIX_CONTAINERCONTOLNAME_ListBox1 and then your work around would fail.
As a general principle its normally considered a bad idea to "hard code" client side ids in your markup - let the ASP.NET process handle it for you
I got it to work.
Be sure that you amend this code to account for the MasterPage contentId prefix:
function LookUpStock()
{
var lb = document.getElementById("MASTERPAGEPREFIX_" + "ListBox1");
var product = lb.options[lb.selectedIndex].text;
CallServer(product, "");
}
function ReceiveServerData(rValue)
{
document.getElementById("MASTERPAGEPREFIX_" + "ResultsSpan").innerHTML = rValue;
}
When I navigate on a website utilizing MasterPages, does the application know what page I am on? If so, does it store it in an object I can access?
The reason I am asking is so I can replace this:
//masterpage
<div id="nav_main">
<ul><asp:ContentPlaceHolder ID="navigation" runat="server">
</asp:ContentPlaceHolder></ul>
</div>
//content page(s)
<asp:Content ContentPlaceHolderID="navigation" ID="theNav" runat="server">
<li>Home</li>
<li id="current">FAQ</li>
<li>Videos</li>
<li>Button 4</li>
<li>Button 5</li>
</asp:Content>
With a more elegant solution for the navigation, which highlights the link to the page by having the list item's ID set to "current". Currently each page recreates the navigation with its respective link's ID set to current.
I'd concur with Chris: use a control to handle display of this menu and make it aware of what link should be highlighted. Here's a method I use regularly. It may become more complex if you've got multiple pages that would need the same link styled differently, but you get the idea.
Dim thisURL As String = Request.Url.Segments(Request.Url.Segments.Count - 1)
Select Cast thisUrl
Case "MenuItem1.aspx"
lnkMenu1.CssClass = "Current"
Case "MenuItem2.aspx"
lnkMenu2.CssClass = "Current"
End Select
To get the current request URL from within the master page you would do:
string s = this.Page.Request.FilePath; // "/Default.aspx"
I also recommend moving your navigation into the master page instead of the content page. This will make it easier to maintain / access.
Yes, Use the below code in your master file. It will give you the content page name.
Page.ToString().Replace("ASP.","").Replace("_",".")
Alternatively you can search for page title if you have set an specific title to the child page instead of masterpage try:
this.Page.Title
Hope it helps.
this is in C#
string thisURL = Request.Url.Segments[Request.Url.Segments.Length - 1];
if (thisURL.ToLower()== "default.aspx") li1.Attributes.Add("class","yekan active");
if (thisURL.ToLower() == "experts.aspx") li2.Attributes.Add("class", "yekan active");
You should be able to get the page by accessing the Page property. IE:
string type = this.Page.GetType().Name.ToString();
You'd probably just use one of the Request path from within the master page to set the current. I'd probably also have a property on the master page to override it, so that pages without links or something could set it to something reasonable.
It worked for me this way - Thanks Jared
This is what I did to get our nav menu to highlight the current menu item for the page that we are viewing. The code is in the master page.
You basically get the filepath (Jared's way)
We use the "~" in our links so I had to strip that out.
Iterate the menuItems collection of the Menu control.
Compare the navigateUrl property.
(I'm not the best coder and even worse at explaining - but it works and I was quite chuffed with it!)
protected void HighlightSelectedMenuItem()
{
string s = this.Page.Request.FilePath; // "/Default.aspx"
string nav;
if (s.Contains("~"))
{
s = s.Remove(s.IndexOf("~"), 1);
}
foreach (MenuItem item in navMenu.Items)
{
if (item.NavigateUrl.Contains("~"))
{
nav = item.NavigateUrl.Remove(item.NavigateUrl.IndexOf("~"), 1);
if (s == nav)
{
item.Selected = true;
}
}
}
}
string s = this.Page.GetType().FullName;
string[] array = s.Split('_');
int count = array.Count<String>();
string currentPage = array[count - 2];
The navigation control, not the master page, should be in charge of what page is currently highlighted.
Either the page that is loaded should notify the navigation item who it is, or the nav control itself should keep track of it.
The point is that master pages are supposed to simply be a holder that content is displayed in. They aren't supposed to control anything.
try
this.Page.Master
It will get you the master page of the current page.
There's also the Request.RawURL