格式化绑定到 Repeater 的某些项目的输出
例如,在后端,我将数据表绑定到中继器,在前端,我将中继器设置为这样:
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<div class="user">
Name: <%# DataBinder.Eval(Container, "DataItem.Name")%>
Email: <%# DataBinder.Eval(Container, "DataItem.Email")%>
Active: <%# DataBinder.Eval(Container, "DataItem.Active")%>
Status: <%# DataBinder.Eval(Container, "DataItem.Status")%>
</div>
</ItemTemplate>
</asp:Repeater>
因此“名称”和“电子邮件”的输出很好。然而,“活动”和“状态”打印出一个整数代码,我想根据我拥有的参考表将其更改为更具描述性的字符串。
我猜我可以在中继器的“ItemDataBound”事件上执行此操作,但我坚持下一步应该做什么,即检查我需要修改和更改它们的两个字段。
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Do modifications here
}
}
For example in the backend I'm binding a datable to a repeater and in the front end I'm setting up my repeater as such:
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<div class="user">
Name: <%# DataBinder.Eval(Container, "DataItem.Name")%>
Email: <%# DataBinder.Eval(Container, "DataItem.Email")%>
Active: <%# DataBinder.Eval(Container, "DataItem.Active")%>
Status: <%# DataBinder.Eval(Container, "DataItem.Status")%>
</div>
</ItemTemplate>
</asp:Repeater>
So the output for "name" and "email" are fine. However "Active" and "Status" print out an integer code that I would like to change to a more descriptive string based on a reference table I have.
I'm guessing I can do this on the "ItemDataBound" event of the repeater, but I'm stuck on what my next step should be, namely checking the two fields that I need to modify and change them.
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Do modifications here
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以
使用选项 1 将要求您声明一个控件(例如标签)来存储每个字段的值,如下所示:
然后在 ItemDataBound 事件中您可以找到该控件并根据需要设置其值。
使用选项 2 将要求您创建一个服务器端可公开访问的方法,您可以像这样调用该方法:
然后定义一个方法,如下所示:
You can either
Using option 1 will require you to declare a control such as a label to store the value for each field like so:
Then in your ItemDataBound event you can find the control and set its value as required.
Using option 2 will require you to create a server side publicly accessible method which you can call like so:
Then define a method like so:
我更喜欢创建在标记中调用的格式方法,而不是处理 ItemDataBound。
然后在你的代码后面:
I prefer creating format methods called in the markup rather than handling ItemDataBound.
Then in your code behind:
你可以这样做:
You could do something like:
无需使用 itemdatabound。只需在 itemtemplate 中添加一个方法即可使用 dataitem.active 作为参数进行转换。添加标签并执行以下操作:
转换是您留在代码后面或执行转换的实用程序类中的方法。
no need to use the itemdatabound. Just add a method in your itemtemplate to do the conversion with the dataitem.active as parameter. Add a label and do the following:
Conversion is then a method you leave in your code behind or utility class where you do the conversion.