自定义中继器中数据源的输出?

发布于 2024-12-08 00:33:54 字数 182 浏览 3 评论 0原文

我有一个连接到数据源(数据表对象)的数据转发器。我需要在某些条件下更改某些列的前端输出。做到这一点最有效的方法是什么?

我目前正在尝试创建格式化输出并将其分配给另一个数据表并将其用作数据源,但它似乎过于复杂并且难以维护。

是否有更简单的方法来操作数据源的列值?我需要能够检查源的上一行和下一行,因为这是某些列值的基础。

I have a Data Repeater hooked up to a datasource (datatable object). I need to change the output on the frontend for certain columns under certain conditions. What would be the most efficient way to do this?

I am currently trying to create the formatted output and assign it to another datatable and use that as the data source, but it seems overly complicated and something that would be hard to maintain.

Is there an easier way to manipulate column values for a datasource? I need the ability to check the previous and next rows for the source as that is a basis for some of the column values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-12-15 00:33:54

如果您正在谈论简单的操作,则 DataBinder.Eval 方法接受格式字符串:

<%#Eval("SomeMoneyColumn", "{0:C}")%>

如果格式字符串不够,您可以在代码隐藏中创建一个方法来处理格式设置,例如this:

<%#FormatData(Eval("SomeColumn"))%>

在代码隐藏中:

protected string FormatData(object data)
{
    return String.Format("My name is {0}", data);
}

您还可以使用 ItemDataBound 事件。使用此技术,如果您的操作涉及绑定到转发器的其他数据,您仍然可以访问数据源对象。

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Label lblCtrl = e.Item.FindControl("SomeControl") as Label;
    if (lblCtrl != null)
    {
        lblCtrl.Text = String.Format("My name is {0}", DataBinder.Eval(e.Item.DataItem, "SomeColumn"));
    }
}

If you're talking about simple manipulation, the DataBinder.Eval method accepts a format string:

<%#Eval("SomeMoneyColumn", "{0:C}")%>

If the format string is not sufficient, you could create a method in the code-behind to handle the formatting, like this:

<%#FormatData(Eval("SomeColumn"))%>

In code-behind:

protected string FormatData(object data)
{
    return String.Format("My name is {0}", data);
}

You can also use the ItemDataBound event too. Using this technique, you can still access the datasource object, in the case that your manipulation involves other data that is bound to the repeater.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Label lblCtrl = e.Item.FindControl("SomeControl") as Label;
    if (lblCtrl != null)
    {
        lblCtrl.Text = String.Format("My name is {0}", DataBinder.Eval(e.Item.DataItem, "SomeColumn"));
    }
}
酷炫老祖宗 2024-12-15 00:33:54

我认为没有一种方法可以在客户端轻松地完成您想要的事情,而无需像您现在所做的那样使用特殊逻辑。如果您从数据库获取数据,您可能会在数据库端执行所有数据操作并将其透明地传递到前端。

I don't think there's a way to do what you want on the client side easily w/o using special logic like you are doing now. If you are getting data from a database, you could potentially do all the data manipulation on the DB side and pass it along transparently to the front end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文