zahed zahed - 1 year ago 122
ASP.NET (C#) Question

Manage Hyperlink base on the condition in Repeater ASP.NET C#

How to manage Hyperlinks base on AttachmentID, In Inside link button there is two hyperlink to manage, If AttachmentID is "NA" Then Hyperlink ID one should visible else Hyperlink ID two should visible. I tried lots into google like this code not able to find. I tried using ItemCommand and ItemDataBound but did not understand this concept. The main concept to do this manage target="_blank".

Below is my Repeater Code.

<asp:Repeater ID="Repeater_News1" runat="server" OnItemDataBound="Repeater_News1_ItemDataBound">
<asp:Image ID="Image2" runat="server" class="pull-left img-responsive" ImageUrl='<%# Bind("ImageName", "~/images/news_images/{0}") %>' />
<asp:LinkButton ID="lnkbtn_check" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Attachmentid") %>'>

<a href='<%# DataBinder.Eval(Container.DataItem, "Attachment")%>' id="one"
<%# DataBinder.Eval(Container, "DataItem.Heading")%></a>

<a href='<%# DataBinder.Eval(Container.DataItem, "Attachment")%>' id="two">

<%# DataBinder.Eval(Container, "DataItem.Heading")%></a>

<%# DataBinder.Eval(Container, "DataItem.SmallDescription")%></p>

Answer Source

Use this in your repeater:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# Eval("AttachmentID").ToString() != "NA" %>' Text='<%# Eval("DataItem.Heading") %>' Target="_blank" />

You can set the Visibility with an if statement in the HyperLink itself: Visible='<%# Eval("AttachmentID").ToString() != "NA" %>'


You can also check the AttachmentID for IsNullOrEmpty and show the correct hyperlink.

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# string.IsNullOrEmpty(Eval("AttachmentID").ToString()) %>' Text='<%# Eval("DataItem.Heading") %>' Target="_blank" />

<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# !string.IsNullOrEmpty(Eval("AttachmentID").ToString()) %>' Text='<%# Eval("DataItem.Heading") %>' Target="_self" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download