Conditional Eval

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)?
JeepGeekinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daniel_ballaCommented:
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")) %>
0
JeepGeekinAuthor 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).
0
daniel_ballaCommented:
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 :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.