Solved

How do I dynamically add _blank to hyperlink control?

Posted on 2011-02-10
6
369 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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