Conditional (if statement) in a datagrid

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.
 
A

amiekweon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top