0
votes

I have a data bound repeater on my ASP.NET page. I am using the repeater to build a list of elements, each one consisting of a text (given by a column in the data bound table) that may be hyperlinked to another page. Whether or not a hyperlink should be included is determined by another column in the data bound table.

I am not sure how to approach the problem. Should I use an HyperLink server control that is shown or hidden depending on the value in the second column? How can I then change the Visible property of this control for each item in the repeater? Are there better approaches?

Thanks.

6
Thanks to all who answered. Many answers are similar, so I accepted as correct the one who I found most clear and to the point. - CesarGon

6 Answers

0
votes

1 Firest approach

Yes you add asp:label and asp:hyerlink to your template, and on ItemDataBound, you access to your data, and adjust visible of your hyperlink and label.

2 Second Approach (Fast)

Or Adjust your property Visible with Eval.DataBinder based on c# expression

For your Hyperelink (Just sample)

Visible='<%#
CustomFunction((object)DataBinder.Eval(Container.DataItem, "YourColumn")).ToString()
%>'
0
votes

You can have two controls. One being a hyperlink and one being a regular label or literal. Then, in the ItemDataBound event, show / hide the appropriate controls based on the value of that second column. See the example code below. You will obviously need to change it to fit your situation.

Private Sub Repeater_ItemDataBound(ByVal sender As Object, Byval e As RepeaterItemEventArgs)
  If (e.Item.ItemType Is ListItemType.Item) Or (e.Item.ItemType Is ListItemType.AlternatingItem) Then
    Dim hyperlink = CType(e.Item.FindControl("HyperLink1"), HyperLink)
    Dim label = CType(e.Item.FindControl("Label1"), Label)
    Dim dataSource = CType(e.Item.DataItem, MyDataSourceObject)

    If (dataSource.HyperLink = String.Empty) Then
      hyperlink.visible = false
    Else
      label.visible = false
      hyperlink.NavigateUrl = dataSource.HyperLink
    End If
  End If
End Sub
0
votes

You can set the visible attribute in your repeater like this:

<asp:HyperLink ID="lnk" runat="server" Visible='<%# Boolean.Parse(Eval("IsVisible").ToString()) %>' />
0
votes

Try this:

void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

          // Execute the following logic for Items and Alternating Items.
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
             DataRowView row = e.Item.DataItem as DataRowView;
             if (row["ColumnName2"]  == "Something") {
                ((HyperLink)e.Item.FindControl("HyperLink1")).NavigateUrl= row["ColumnName1"];
             }
             else
             {
               ((HyperLink)e.Item.FindControl("HyperLink1")).Visible = False;
             }
          }
       }
0
votes

Use Repeater ItemDataBound Command..

 void repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
   {                          
      DataRowView row = (DataRowView)e.Item.DataItem;

      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType    ==ListItemType.AlternatingItem) 
       {             
            if(row["othercolumnName"].ToString()==true)//You can check the value here..use your logic
             {
                LinkButton lnk=(LinkButton)e.Item.FindControl("lnkID");
                 lnk.Visible=False;
             }
      }
  }     
0
votes

I prefer to do this sort of simple thing in the markup because it's easier to test, modify and accommodate changes in the future.

Here's an example of how to do it - assume I am binding to a collection of objects with a Name, a ShouldWeCreateLink and an Address property:

<ItemTemplate>
        <tr>
            <td>
                <%--Bind to Name Property--%>
                <%#  Eval("Name") %>
            </td>
            <%--Should we show a hyperlink or a simple text with the address?--%>
            <td>
               <%# Eval("ShouldWeCreateLink").ToString() == "True" ? string.Format("<a href=\"Link.aspx?ID={0}\">{1}</a>", Eval("ID"),"Click me") : Eval("Address")%>
            </td>
        </tr>
    </ItemTemplate>

The above would generate a hyperlink like this when it needs to create a hyperlink:

<a href="link.aspx?ID=9">Click me</a>

Avoid as much as you can to use server-controls unless you require ViewState or programmatic access to their properties from code-behind.