问题
In Sitecore I have a General Link which is set as an external link. What is the simplest way for me to render this link so that I get target="_blank" on my page and it opens in a new window?
回答1:
If I understand you correctly, you have a General Link field, and you'd like it to hold a reference which is to open in a new window. If that's the case, the image below should answer your question:
回答2:
In addition to what Yan said about configuring the link in Sitecore, if you need code to render it, you can do it with a FieldRenderer or in C# with the API to access the field.
e.g.
<sc:Link Field="Your Field Name" runat="server" />
or
<sc:FieldRenderer FieldName="Your Field Name" runat="server" />
回答3:
You can do this in code as well, in the case you have a framework built over the Sitecore API
Sitecore.Web.UI.WebControls.FieldRenderer fieldRenderer =
new Sitecore.Web.UI.WebControls.FieldRenderer();
fieldRenderer.Item = MySitecoreItem;
fieldRenderer.FieldName = "Link";
return fieldRenderer.Render();
The "Link" hardcode is specific to Sitecore for the system supplied Link template and you can reuse that in your code.
MySitecoreItem is a type of Sitecore.Data.Items.Item.
I encountered the problem where I could get the URL from the item by saying LinkManager.GetItemUrl(), but I couldn't get the css classes that were embedded in the link template without getting out regex or String.Split() after asking for the Item.Field[0].Value.
The above method rendered the link out to a string that I could write directly to the browser and would work as you'd expect an anchor tag to do.
来源:https://stackoverflow.com/questions/11874079/render-a-general-link-in-sitecore-with-target-blank