views:

409

answers:

2

Dear All,

how would you go about adding logic to a datagrid item template? In my datagrid, i want to add a logic to it. that is, if the result for the data equals to "Yes", an "asp:label" control will be displayed; otherwise a "asp:imagebutton" control will be shown

<ItemTemplate1> 
<% if DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") then%>
<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"boflag")%>'></asp:Label>
<% Else %>
<asp:imagebutton id="imgBtnUpdate" runat="server" NAME="Imagebutton3"
ImageUrl="no.gif"></asp:imagebutton>
<% end if %>
</ItemTemplate>

However, "<% if DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") then%> " this is not valid.

So, how can i get the data to compare the value.

Thank you

A: 

You should implement the items Data Bound Event in the code behind. Then show/hide/populate the controls there.

Mark Redman
A: 

One other option you can do is to use a ternary operator to evaluate the boflag field and output accordingly. For example:

<%# DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") ?  DataBinder.Eval(Container.DataItem,"boflag") : "<input type=\"image\" src=\"\" />" %>

I'm not sure that you could add server controls through this method, but you could certainly add conditional HTML.

joe.liedtke