Solved

How do I dynamically add _blank to hyperlink control?

Posted on 2011-02-10
6
372 Views
Last Modified: 2012-05-11
Presently in my mark up I have a hyperlink control written as so (see the code). I'd like for it to open in a new window, based on whether or not there is a document associated with a particular record (see function, also in code section) or not. How do I accomplish this?
------------MARKUP---------

                        <asp:HyperLink runat="server" NavigateUrl='<%# GetDocOrPage(Container.DataItem as DigitalLagoon.BL.NewsRelease) %>'> "_blank" would go here if i wanted it to do it all the time.
                            <asp:Literal ID="lblArticleText" runat="server" Text='<%# GetArticlePreview(Eval("HeadlineText")) %>' />
                        </asp:HyperLink>

---------C# FUNCTION-------------

    protected string GetDocOrPage(NewsRelease newsRelease)
    {

        if (newsRelease != null)
        {
            if (newsRelease.HasLinkToDocument)
            {
                if (!String.IsNullOrEmpty(newsRelease.DocumentLink))
                {
                    string path = Path.Combine(SiteSettings.DocumentsVirtualPath, newsRelease.DocumentLink).Replace("\\", "/"); <--i believe this line is where i'll need to add the "_blank"
                    if (File.Exists(Server.MapPath(path)))
                        return path;
                }

            }

            if (newsRelease.NewsReleaseUid > 0)
                return "/displayNewsRelease.aspx?NewsReleaseUid=" + newsRelease.NewsReleaseUid;
        }
        return "newsReleases.aspx";

    }

Open in new window

0
Comment
Question by:mikesExpertExchange
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34867951
Since you're doing data binding, you'll have to do it in one of the data-binding events for the containing control. Which data control is the above markup contained by?
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 34868042
@kaufmed: see code below
<asp:Repeater ID="rptNewsReleases" runat="server" DataSourceID="dsNewsReleaseList"
            OnItemDataBound="rptNewsReleases_ItemDataBound">
            <HeaderTemplate>
                <ol class="dl-news-list">
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <p class="dl-news-release-dt">
                        <asp:Literal runat="server" Text='<%# Eval("ReleaseDate","{0:MM.yyyy}") %>' />
                        &nbsp
                        <asp:HyperLink runat="server" NavigateUrl='<%# GetDocOrPage(Container.DataItem as DigitalLagoon.BL.NewsRelease) %>' Target="_blank">
                            <asp:Literal ID="lblArticleText" runat="server" Text='<%# GetArticlePreview(Eval("HeadlineText")) %>' />
                        </asp:HyperLink>
                        <asp:HyperLink runat="server" NavigateUrl='<%# String.Format("/displayNewsRelease.aspx?NewsReleaseUid={0}", Eval("NewsReleaseUid")) %>'
                            Text="More" /></p>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ol>
            </FooterTemplate>
        </asp:Repeater>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34868140
Try this; you already have the ItemDataBound handler declared. Within that handler, try something like the following. I have used 0 as the indexer, but try adjusting your indexes according to the position of each "runat=server" tagged tag (server tags basically). For instance, I would say the Literal control is index 0, the first Hyperlink is 1, and the second HyperLink is 2.
protected void rptNewsReleases_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    (e.Item.Controls[0] as HyperLink).Target = "_blank";
}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 34872655
and it this on the rendering of the page or after? i need this to happen when the user clicks on the link.
0
 
LVL 1

Accepted Solution

by:
mikesExpertExchange earned 0 total points
ID: 34873322
figured it out. added a function to the Target property of the hyperlink control. i passed the current data item and based on a value of one of its members i either returned "_blank" or "" (also blank ;-) ). and that did the trick. thanks for your time and help.
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 34904807
figured it out. added a function to the Target property of the hyperlink control. i passed the current data item and based on a value of one of its members i either returned "_blank" or "" (also blank ;-) ). and that did the trick. thanks for your time and help.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question