Solved

How do I dynamically add _blank to hyperlink control?

Posted on 2011-02-10
6
373 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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