PDA

View Full Version : ASP.net datagrid question


david7777
03-18-2003, 12:30 PM
I have a datagrid called searchGrid
I have data bound it with a filled dataset. Everything worls fine, but i need a condition in there...

I have something like this:


<asp:DataGrid id="SearchGrid" runat="server" AutoGenerateColumns="false" PageSize="5" AllowPaging="true">
<Columns>
<asp:BoundColumn DataField="id" Visible="False" />
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "short_desc") %>
<br />
<% if (DataBinder.Eval(Container.DataItem, "discount")) > 0 then %>
Was R <%# DataBinder.Eval(Container.DataItem, "price") %>
<br />
Now R <%# get_Price_After_Discount(DataBinder.Eval(Container.DataItem, "price"), DataBinder.Eval(Container.DataItem, "discount")) %>
<br />
<% else %>
R <%# DataBinder.Eval(Container.DataItem, "price") %>
<br />
<% end if %>
<asp:Button Text="Add To Cart" runat="Server" />
<br />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


The if part:

<% if (DataBinder.Eval(Container.DataItem, "discount")) > 0 then %>
Was R <%# DataBinder.Eval(Container.DataItem, "price") %>
<br />
Now R <%# get_Price_After_Discount(DataBinder.Eval(Container.DataItem, "price"), DataBinder.Eval(Container.DataItem, "discount")) %>
<br />
<% else %>
R <%# DataBinder.Eval(Container.DataItem, "price") %>
<br />
<% end if %>


does not work. Why not? I get the error:
Name 'Container' is not declared.

<%# DataBinder.Eval(Container.DataItem, "price") %> works, but <% if (DataBinder.Eval(Container.DataItem, "discount")) > 0 then %> does not.

How do I access the information from the datagrid for use in an if statement? Or is there a better way?

david7777
03-18-2003, 12:32 PM
Sorry about the smilie faces there :D

The VB code must have got it...

david7777
03-18-2003, 01:06 PM
Its ok - I figured it out.

I just put the if statement in a server side function, and passed the data i needed to the function to do the work. For some reason that worked... Still dont know why though?

allida77
03-18-2003, 06:31 PM
I normally do functionality like that in my onItemDataBound Method (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclassonitemdataboundtopic.asp)