在数据绑定控件中应用条件格式化时的最佳做法? [英] Best practices when applying conditional formatting in data bound controls?

查看:77
本文介绍了在数据绑定控件中应用条件格式化时的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与自定义对象(EntitySpaces查询)绑定的Repeater轮廓,并注意到有几种方式有条件地格式化显示的值。

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中,我可以在代码隐藏中调用一个方法,并通过绑定值并使用它来驱动任何条件逻辑:

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"]);
        }
    }

我也注意到使用e.Item。代码隐藏中的FindControl()需要在aspx中的控件上使用runat =server,这个控件具有编码ids的恶作剧习惯,并且通常会弄乱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.

这篇关于在数据绑定控件中应用条件格式化时的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆