Saturday, March 24, 2012

Operations Inside Template Columns?

I have a datagrid with template columns. I can get information from my query and bind to the data. BUT I want to run an operation on the data.

strToday is already defined above the html. How can I put the databinder 'expiration date' in a variable so I can run the DateDiff Function using the bound data for that cell? I tried something simple like strexpdate = databinder.eval yadada, but it wont work. I know I'm missing something fundamental here. I'm just a noob.

<asp:TemplateColumnHeaderText="Days Remaining">

<ItemTemplate>

<%#DataBinder.Eval(Container.DataItem,"expirationdate")%> 'need to take this data to the next line and perform function

<%=DateDiff("d", strToday,"07/06/1980") * -1%>

<%=dtNow.Today%>

<%=strtoday%>

</ItemTemplate>

</asp:TemplateColumn>

This is C# VS2005, but should give you an idea of how to do it. It uses a connection to teh Northwind database

<%@.PageLanguage="C#" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="OrderID"

DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundFieldDataField="OrderID"HeaderText="OrderID"InsertVisible="False"ReadOnly="True"

SortExpression="OrderID"/>

<asp:BoundFieldDataField="OrderDate"HeaderText="OrderDate"SortExpression="OrderDate"/>

<asp:TemplateField>

<ItemTemplate>

Two Days Later =

<%# ((DateTime) Eval("OrderDate")).AddDays(2).ToString() %>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT [OrderID], [OrderDate] FROM [Orders]"></asp:SqlDataSource>

</form>

</body>

</html>


No, I think you are missing the point entirely. So I'll simplify it.

I pull sql data. One column is a date value called 'expiringdate'. I want to use a template column that pulls this data and performs and operation, and then displays the results. But I can't do this outside the template column because the data wont exsist yet.

I know I can mimic the bound column by doing a databinder thing. But what if I wanted to:

get databinded value, manipulate this value, then output the manipulated value. I know how to do this in the code section but not the design section. I had thought it would be like this quasi code, but it does not work.:

<% <%=DateDiff("d", strToday,DataBinder.Eval(Container.DataItem,"expirationdate")) * -1%>


Apologies if I am still missing the point. I have included an in-line SQL and in-line ASPX code to do what I think you want.

If you want further customisation in SQL, the following should work.

Create an SQL string dynamically for use in the data source in the ASPX page, adding the columns you need with the required calculations.

Pass the SQL for a particular column calculation to a stored procedure that "pastes" it into the SQL to be executed, to return the required data.

<%@.PageLanguage="C#" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="OrderID"

DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundFieldDataField="OrderID"HeaderText="OrderID"InsertVisible="False"ReadOnly="True"

SortExpression="OrderID"/>

<asp:BoundFieldDataField="OrderDate"HeaderText="OrderDate"SortExpression="OrderDate"/>

<asp:TemplateField>

<ItemTemplate>

Days Since (Using DateDiff in SQL) =

<%# Eval("DateDiff") %>

<br/>

Days Since (Using Inline Code) =

<%# (DateTime.Now - (DateTime) Eval("OrderDate")).Days %>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="Select Top 5 OrderId, OrderDate, DateDiff(day, OrderDate, GetDate()) as DateDiff FROM Orders">

</asp:SqlDataSource>

</form>

</body>

</html>


I feel stupid... It was the <%= %> I should have used <%# %> strToday is defined in the code by pulling the server date.

So the line is:

DateDiff("d", DataBinder.Eval(Container.DataItem,"expirationdate"), strToday) * -1)


Not as stupid as the European space agency that tried to coerce a 32 bit number into 16 bit field in a rocket lauch guidance software package. Ouch! What goes up...
In this case nothing went up lol.

it is also possible to call public/protected methods from your code-behind in your databind statements

<%# someCustomMethod(DataBinder.Eval(Container.DataItem,"expirationdate")) %>

0 comments:

Post a Comment