在数据应用最佳实践时,条件格式绑定控件?绑定、控件、条件、格式

2023-09-04 02:32:08 作者:我听闻你始终一个人

我有一个中继器控逆变绑定到自定义对象(EntitySpaces查询),并已注意到有几个方法可以有条件地格式化显示的值。

I have a Repeater contol bound to a custom object (an EntitySpaces query) and have noticed there are a couple of ways to conditionally format the values being displayed.

1)从我的aspx我可以在我的$ C $调用方法C-后面,穿过边界值,并用它来驱动任何条件逻辑:

1) From my aspx I can call a method in my code-behind and pass through the bound value and use that to drive any conditional logic:

        <a class="star" href="<%#MakePackageSelectionUrl((int)DataBinder.Eval(Container.DataItem, "PackageId"))%>">

and then in the code-dehind:

    protected string MakePackageSelectionUrl(int packageId)
    {
              return string.Format("/Packages/NoAjax/ToggleStar.aspx?p={0}&amp;s={1}&amp;st={2}", packageId, _streamId, (int)_phase);
    }

2)我可以挂接到ItemDataBound事件,检索e.Item.DataItem作为一个DataRowView的,然后发疯:

2) I can hook into the ItemDataBound event, retrieve e.Item.DataItem as a DataRowView and then go crazy:

    protected void PackageList_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) { return; }

        DataRowView details = (DataRowView)e.Item.DataItem;

        EncodePackageName(e, details);
        EncodeStatusName(e);
        DisplayStarImage(e, details);
    }

    private static void EncodePackageName(RepeaterItemEventArgs e, DataRowView dr)
    {
        HtmlAnchor p = (HtmlAnchor)e.Item.FindControl("packageLink");
        if (p != null)
        {
            p.HRef = string.Format("/Packages/View.aspx?p={0}", dr["packageId"]);
            p.InnerHtml = HttpUtility.HtmlEncode((string)dr["packageName"]);
        }
    }

我也注意到,在$ C $使用e.Item.FindControl()C-落后,需要在有编码的ID,一般搞乱了的一个坏习惯的ASPX控制=服务器 HTML

I've also noticed that using e.Item.FindControl() in the code-behind requires runat="server" on the control in the aspx which has a nasty habit of encoding ids and generally messing up the HTML.

我渴望从任何人想出了一个很好的方法来处理这​​些类型的问题听。

I'm keen to hear from anyone that has come up with a nice approach for dealing with these sorts of issues.

推荐答案

在这种情况下,你正在做的是操纵一些HTML,所以我会用第一种方法。第二种方法是适当的,你需要检查的项目是数据绑定和更改服务器控件响应(例如结合嵌套列表)。

In this case all you are doing is manipulating some HTML so I would use the first method. The second method is appropriate where you need to check the item being databound and make changes to server controls in response (for example binding nested lists).

还要注意,调用的DataBinder.Eval()是昂贵的 - 它使用反射。您将使用显式类型转换,像这样获得更好的性能:

Note also that calls to DataBinder.Eval() are expensive - it uses reflection. You will get better performance using explicit casting like so:

MakePackageSelectionUrl(((System.Data.DataRowView)Container.DataItem)["PackageId"])

有关参考: http://msdn.microsoft.com/en-us /library/ms998549.aspx 。见最大限度地减少调用的DataBinder.Eval。

For reference: http://msdn.microsoft.com/en-us/library/ms998549.aspx. See section on minimising calls to DataBinder.Eval.

 
精彩推荐