在GridView中使用Eval If / Else / EndIf会导致错误数据绑定方法,如Eval(),XPath()和Bind()

时间:2014-06-06 10:23:01

标签: asp.net gridview eval databinder

我正在尝试根据返回的数据库值以编程方式更改GridView中的值。

数据库值“is_zero_minutes_task”是布尔值。如果为True我希望显示“0”,如果为False,我希望显示“MinutesTaken”中返回的值。

我的代码:

    <asp:SqlDataSource ID="SqlDataSourceRecentJobs" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString_LIVE_customer_support %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString_LIVE_customer_support.ProviderName %>" 
        SelectCommand="SELECT customer_name, start_time, end_time, is_zero_minutes_task, TIMESTAMPDIFF(MINUTE,start_time,end_time) AS MinutesTaken FROM time_recorder_jobs WHERE (time_recorder_jobs.deleted = @deleted) AND (time_recorder_users.company_id = @companyid) ORDER BY end_time">
        <SelectParameters>
            <asp:Parameter Name="@deleted" DefaultValue="0" Type="Object" />
            <asp:SessionParameter Name="companyid" SessionField="CompanyID" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

    <asp:GridView ID="GridViewRecentJobs" runat="server" 
        AutoGenerateColumns="False" CellPadding="4" 
        DataSourceID="SqlDataSourceRecentJobs" ForeColor="#333333" 
        GridLines="None" CellSpacing="4" ShowHeaderWhenEmpty="True"
        AllowSorting="True" AllowPaging="True">
        <Columns>
            <asp:BoundField DataField="customer_name" HeaderText="customer_name" 
                SortExpression="customer_name">
                <ItemStyle Wrap="False" />
            </asp:BoundField>
            <asp:BoundField DataField="start_time" HeaderText="start_time" 
                SortExpression="start_time">
                <ItemStyle Wrap="False" />
            </asp:BoundField>
            <asp:BoundField DataField="end_time" HeaderText="end_time" 
                SortExpression="end_time">
                <ItemStyle Wrap="False" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="MinutesTaken" SortExpression="MinutesTaken">
                <ItemTemplate>
<%If Eval("is_zero_minutes_task").ToString = True Then%>
                    <asp:Label ID="MinutesTaken" runat="server" Text="0"></asp:Label>
<%Else%>
                    <asp:Label ID="MinutesTaken" runat="server" Text='<%# Bind("MinutesTaken") %>'></asp:Label>
<%End If%>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我也尝试过使用:

<%If DataBinder.Eval(GridViewRecentJobs.DataItem, "is_zero_minutes_task") = True Then%>

但是这表示错误“'DataItem'不是System.Web.UI.WebControls.GridView的成员'”

1 个答案:

答案 0 :(得分:1)

我解决了!

<asp:TemplateField HeaderText="MinutesTaken" SortExpression="MinutesTaken">
<ItemTemplate>
    <asp:Label ID="MinutesTaken" runat="server" Text='<%# If((Eval("is_zero_minutes_task") = 1), "0", Eval("MinutesTaken"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>