Link to home
Create AccountLog in
Avatar of JeepGeekin
JeepGeekin

asked on

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)?
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of daniel_balla
daniel_balla

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")) %>
Avatar of JeepGeekin

ASKER

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 :)