是否有一个ASP.NET数据可绑定控件(如Repeater),该控件仅需要一个项目? [英] Is there an ASP.NET databindable control (like Repeater), which only expects a single item?

查看:46
本文介绍了是否有一个ASP.NET数据可绑定控件(如Repeater),该控件仅需要一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Web窗体上显示业务对象的许多属性.我当然可以创建标签的负载,并在后台代码中分配所有值,但是我觉得必须有一种更简洁的方法.

I'm trying display lots of properties from a business object on a Web Form. I could of course, create loads of labels and assign all the values in code-behind, but I feel there must be a less verbose way.

我想要的是类似asp:Panel的东西,但是您可以在其中指定数据源.似乎Panels不支持任何类型的数据绑定.

What I want is something like an asp:Panel but where you can specify a datasource. It doesn't seem like Panels support any kind of databinding.

我想要的是这样的东西

// C#
panel.DataSource = theCompany;
panel.DataBind();

然后:

// ASPX

<asp:Panel runat="server">
    Name: <%# Eval("Name") %>
    Phone: <%# Eval("Phone") %> 
    ...
</asp:Panel>

..但是我找不到任何可以使我以这种方式工作的东西.

..but I can't find anything which will allow me to work in this way.

我以为我也许可以使用 asp:FormView ,但这只会出现错误数据源是无效类型.它必须是IListSource,IEnumerable或IDataSource."

I thought I might be able to use asp:FormView but this just gives the error "Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource."

一个警告是,我不想要调用全局DataBind()(这在过去使我们无休止地遇到了很多问题)-我希望将databind限制为页面的特定部分.

A caveat is that I do not want to call a global DataBind() (this has caused us no-end of problems in the past) - I would like the databind to be constrained to a particular part of the page.

推荐答案

似乎可以使用面板执行此操作,但是您必须首先将业务对象分配给page属性,因为无法设置业务对象作为面板的数据源"(例如,对于Repeater控件而言).

It seems you can do this using a Panel, but you have to assign your business object to a page property first, as there's no way to set the business object as a "DataSource" for the panel (as you would for a Repeater control, for instance).

将对象分配为页面属性后,您便可以在.aspx中使用以下语法访问该对象的属性,而无需手动分配每个项目以控制后面代码中的值:

Once the object is assigned as a page property, you can then use the following syntax in the .aspx to access the properties of that object, without needing to manually assign each item to control values in code behind:

<%# Company.Name %>

这篇关于是否有一个ASP.NET数据可绑定控件(如Repeater),该控件仅需要一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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