We help IT Professionals succeed at work.

Conditional Eval

JeepGeekin
JeepGeekin asked
on
Medium Priority
3,390 Views
Last Modified: 2012-05-05
How do I add a conditional argument to a gridview? Basically, I need to check the value in a column to determine if another column will become a hyperlink or just display plain text. It seems like I should be able to alter this:

<asp:templatefield>
<itemtemplate>
      <asp:HyperLink" runat="server" NavigateUrl="whatever">
             <%#Eval("DisplayableTextColumn")%>
      </asp:HyperLink>        
</itemtemplate>
</asp:templatefield>

...To be something like this, but I'm not getting useful help from the error (other then it doesn't like what I have done with the eval):

<asp:templatefield>
<itemtemplate>
                                         
<% IF #Eval("ColumnToEvaluate") = 0 Then %>
         <asp:HyperLink" runat="server" NavigateUrl="whatever">
                <%#Eval("DisplayableTextColumn")%>
          </asp:HyperLink>        
<% ELSE %>
           <%#Eval("DisplayableTextColumn")%>
<% END IF%>

</itemtemplate>
</asp:templatefield>

Also, is this a good way to do this? I'm not sure if it's efficient. Or would it be more efficient to handle this elsewhere (like in the code behind on the gridview databind event or something)?
Comment
Watch Question

Top Expert 2006
Commented:
Hi JeepGeekin,

I think the way I would do this, and keep it clean is to put all the controls you want in your ItemTemplate and then determine their Visible property in the code-behind, within the RowDataBound event:

If e.Row.RowType = DataControlRowType.DataRow Then
    Dim row As DataRowView = CType(e.Row.DataItem, DataRowView)
    Dim HyperLink1 As HyperLink = CType(row.FindControl("HyperLink1"), HyperLink)
    Dim Label1 As Label = CType(row.FindControl("Label1"), Label)

    If row("DisplayableTextColumn") = "Something" Then
        HyperLink1.Visible = False
        Label1.Visible = True
    Else
        HyperLink1.Visible = True
        Label1.Visible = False
    End If
End If

Something like that, just remember depending on what data source you are binding to the GridView, you might have to change the DataRowView to the appropriate object for you... Good luck!

-- Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
or, you could use inline code with IIF but instead of rendering and asp:HyperLink control you would render an <a href... one. such as
<%= IIF(DataBinder.Eval(Container.DataItem, "columnName") = 0, "<a href='blahblah'>"&DataBinder.Eval(Container.DataItem, "anotherColumnName"), DataBinder.Eval(Container.DataItem, "yetAnotherColumnName")) %>

Author

Commented:
lol. This is about the 10th issue you've helped me with Jason. Thanks! As always you provide simple, straight forward solutions while thinking outside the box.

Daniel, I'll see if I can get you some points. I didn't know that I could pull in the eval using DataBinder. You answered my question. I just don't like merging conditions into the ASP code if I can avoid it (though I didn't say that).
JeepGeekin,
> Daniel, I'll see if I can get you some points. I didn't know that I
> could pull in the eval using DataBinder. You answered my question. I
> just don't like merging conditions into the ASP code if I can avoid it
> (though I didn't say that).

So do I, and I think it's a good practice to avoid inline code, but it's a personal opinion and others disagree :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.