Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I dynamically add _blank to hyperlink control?

Posted on 2011-02-10
6
Medium Priority
?
376 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:Michael Sterling
[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:Michael Sterling
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:Michael Sterling
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:
Michael Sterling 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:Michael Sterling
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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