Conditional (if statement) in a datagrid

  • Thread starter Thread starter maflatoun
  • Start date Start date
M

maflatoun

Hi,

I have a datagrid and based on a condition I like to display either a
asp:textbox or asp:label. How would I do that? I have the following

<asp:TemplateColumn HeaderText="Qty">
<ItemTemplate>
<% if (DataBinder.Eval(Container.DataItem, "DynamicAttribute") = "No")
{%>

<asp:textbox id="tbxQtyNew" BackColor='<%#
SetTextBoxColor(DataBinder.Eval(Container.DataItem,
"DynamicAttribute")) %>' runat="server" text='1' />
<% } else { %>
<asp:Label id="lblQtyDisp" Text="1" runat="server"/>
<% } %> </ItemTemplate>
</asp:TemplateColumn>

Which fails. Any idea?

Thanks
Maz.
 
It might be easier to do it from code-behind. Alot cleaner and more
efficient.
Use OnItemDataBound event and put all of logic in ItemDataBound
handler.

Leave both textbox and label controls on .aspx page with their Visible
property set to False. Then from the code-behind, do something like:

public void HandleItemDataBound(object sender, DataGridItemEventArgs e)

{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
SomeClass oItem = (SomeClass) e.Item.DataItem;
if (oItem.SomeProperty == "SomeValue") {
// show label
} else {
// show textbox
}
oItem = null;
}
}

Hope it helps!

Amie
 
Back
Top