Reputation: 21
i am using response.redirect to hold the url,which opens link on same page, what can be other to open link in new tab?
public partial class DocumentLink : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
try
{
Sitecore.Data.Fields.LinkField linkField = SitecoreItem.CurrentItem.Fields["DocumentLink"];
Response.Redirect(GetUrl(linkField));
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(ex.ToString(), this);
}
}
}
public string GetUrl(Sitecore.Data.Fields.LinkField LinkField)
{
try
{
string url = "";
switch (LinkField.LinkType)
{
case "internal":
case "external":
case "mailto":
case "anchor":
case "javascript":
url = LinkField.Url;
break;
case "media":
MediaItem media = new MediaItem(LinkField.TargetItem);
url = Sitecore.StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(media));
break;
case "":
default:
break;
}
return url;
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(ex.ToString(), this);
throw;
}
}
}
Upvotes: 2
Views: 713
Reputation: 114
You can use Sitecore's link webcontrol and pass field name to it, Sitecore will automatically render all attributes of your GenralLinkField
<sc:Link runat="server" Field="DocumentLink">
Upvotes: 0
Reputation: 16990
Why do you need the redirect? Do you not render a link to the document on the page?
If so I would just add a css class to the link and use some jQuery to make all those links open in a new window.
Open a link in a new window using jQuery
$(document).ready(function(){
$('.external-link').click(function(event){
event.preventDefault();
window.open(this.href);
});
});
Note, you can't force a link to open in a new tab, it depends on the users preference as to whether they want new windows to open in tabs or not.
Upvotes: 1
Reputation: 27132
You need to execute javascript code to open a new tab. This can be executed using the code:
ClientScript.RegisterStartupScript(
this.GetType(),
"newTab" + DateTime.Now.Ticks,
String.Format("<script>window.open('{0}');</script>", GetUrl(linkField)));
Here you can find some more information on adding client scripts dynamically http://msdn.microsoft.com/en-us/library/ms178207.aspx.
Remember that browsers may block new windows.
Upvotes: 1