Solved

How do I dynamically add _blank to hyperlink control?

Posted on 2011-02-10
6
368 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
  • 4
  • 2
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
@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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now