如何<%# Eval() %> 并更改日期

本文关键字:日期 Eval 如何 | 更新日期: 2023-09-27 18:33:17

仅当从数据库中检索某些内容时,我才尝试添加带有参数的链接。

这是我的代码:

protected void GetOrderLines (string OrderID)
{
    string query;
    query = "SELECT ol.ItemId, ol.Amount, ol.Quantity, i.description, ol.RetailPrice, o.OrderDate";
    query += " FROM Distributor d";
    query += " INNER JOIN Orders o";
    query += " ON o.DistID = d.DistID";
    query += " INNER JOIN Orderlines ol";
    query += " ON ol.OrderID = o.OrderID";
    query += " INNER JOIN Items i";
    query += " ON i.InventoryID = ol.ItemID";
    query += " WHERE ol.OrderID = " + OrderID;
    query += " AND ol.GroupItem = 0";
    query += " AND d.DistID = " + Session[ "Distid" ];
    DataTable data = GeneralFunctions.GetData( query );
    RepeaterOrderlineInfo.DataSource = data;
    RepeaterOrderlineInfo.DataBind();
}

这是它给我的数据:

ItemId  Amount  Quantity    description RetailPrice OrderDate
6015    660 1   Item 1  660 5/1/2012
6021    199.2   332 Item 2  0.6 5/1/2012
6018    150 6   Item 3  25  5/1/2012
9000    85  4   Technical Support   21.25   5/1/2012
8000    125 4   Custom Programming and Misc. Fees   31.25   5/1/2012

这是页面上的评估代码:

<asp:Repeater ID="RepeaterOrderlineInfo" runat="server">
    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("Itemid") %>
            </td>
            <td>
                <%# Eval("description") %>
            </td>
            <td align="right">
                <%# Eval("RetailPrice", "{0:C}") %>
            </td>
            <td align="right">
                <%# Eval("Quantity") %>
            </td>
            <td align="right">
                <%# Eval( "Amount", "{0:C}" )%>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

我正在尝试为<%# Eval("Description") %>建立一个链接

<a href="SupportSummary.aspx?sd="<%# Eval("OrderDate") %>&ed=<%# Eval("OrderDate") //- 1 month %>"><%# Eval("Description") %>

我只需要描述是一个链接,如果description = "Technical Support". 使用中继器时可以这样做吗?

所以我在这里寻求的主要内容是:

如何评估订单大日并减去一个月。如何仅在描述="技术支持"时才对描述执行条件。

如何<%# Eval() %> 并更改日期

您确实需要使用中继器切换到 OnItemCreated 才能对输出的标记进行大量控制。这很容易做到,如下所示:

<asp:Repeater ID="RepeaterOrderlineInfo" runat="server">
    <ItemTemplate>
        <asp:Label ID="labelDate" runat="server" />
    </ItemTemplate>
</asp:Repeater>
RepeaterOrderlineInfo.ItemCreated += Repeater_ItemCreated;
protected void Repeater_ItemCreated(object sender, RepeatrItemCreatedEventArgs e)
{
    DataRow dataItem = e.Item.DataItem as DataRow;
    if(dataItem != null)
    {
        Label labelDate = e.Item.FindControl("labelDate") as Label;
        if(labelDate != null && row["Description"] != "TechnicalSupport")
           labelDate.Text = // whatever you want to do
    }
}

这为您提供了制作餐桌的极大灵活性。老实说,我会尽量避免在您的标记中Eval(<string>),而是在您的代码中处理大部分问题。

如果将

方法添加到代码隐藏中,则可以在中继器中像这样调用它:

<%# MyMethod(Eval("Description"),Eval("OrderDate")) %>

该方法如下所示:

protected string MyMethod(object description, object orderDate)
{ 
    string link = String.Empty;
    // conditions, assign link if needed
    return link;
}

Tejs和Mafue的答案是正确的,但你也可以这样做>>

<%# Eval("Description").ToString().Equals("Technical Support", StringComparison.OrdinalIgnoreCase) ? link : String.Empty %>