Conditional Eval

JeepGeekin
JeepGeekin used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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
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 :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial