格式化绑定到 Repeater 的某些项目的输出

发布于 2024-12-13 11:44:21 字数 978 浏览 0 评论 0原文

例如,在后端,我将数据表绑定到中继器,在前端,我将中继器设置为这样:

<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 技术交流群。

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

发布评论

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

评论(5

鸠书 2024-12-20 11:44:21

您可以

  1. 在 ItemDataBound 事件中处理格式
  2. 在 Page 或 WebUserControl 类中创建公共方法来处理格式。

使用选项 1 将要求您声明一个控件(例如标签)来存储每个字段的值,如下所示:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
             <asp:Label ID="ActiveLabel" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:Label>
     </div>
    </ItemTemplate>
</asp:Repeater>

然后在 ItemDataBound 事件中您可以找到该控件并根据需要设置其值。

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
            Label activeLabel = (Label)e.Item.FindControl("ActiveLabel");

            //Format label text as required
    }
}

使用选项 2 将要求您创建一个服务器端可公开访问的方法,您可以像这样调用该方法:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
     Active: <%# FormatActive((string)DataBinder.Eval(Container, "DataItem.Active")) %>
     </div>
    </ItemTemplate>
</asp:Repeater>

然后定义一个方法,如下所示:

public string FormatActive(string input)
{
     //Format as required
     //Return formatted string
}

You can either

  1. Handle the formatting in the ItemDataBound event
  2. Create public methods in your Page or WebUserControl class to handle the formatting.

Using option 1 will require you to declare a control such as a label to store the value for each field like so:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
             <asp:Label ID="ActiveLabel" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:Label>
     </div>
    </ItemTemplate>
</asp:Repeater>

Then in your ItemDataBound event you can find the control and set its value as required.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
            Label activeLabel = (Label)e.Item.FindControl("ActiveLabel");

            //Format label text as required
    }
}

Using option 2 will require you to create a server side publicly accessible method which you can call like so:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
     Active: <%# FormatActive((string)DataBinder.Eval(Container, "DataItem.Active")) %>
     </div>
    </ItemTemplate>
</asp:Repeater>

Then define a method like so:

public string FormatActive(string input)
{
     //Format as required
     //Return formatted string
}
浅黛梨妆こ 2024-12-20 11:44:21

我更喜欢创建在标记中调用的格式方法,而不是处理 ItemDataBound。

<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: <%# FormatActive((int)DataBinder.Eval(Container, "DataItem.Active"))%>
         Status: <%# FormatStatus((int)DataBinder.Eval(Container, "DataItem.Status"))%>
     </div>
    </ItemTemplate>
</asp:Repeater>

然后在你的代码后面:

protected static FormatActive(int active)
{
    return "Formated Active String...";
}

protected static FormatStatus(int status)
{
    return "Formated StatusString...";
}

I prefer creating format methods called in the markup rather than handling ItemDataBound.

<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: <%# FormatActive((int)DataBinder.Eval(Container, "DataItem.Active"))%>
         Status: <%# FormatStatus((int)DataBinder.Eval(Container, "DataItem.Status"))%>
     </div>
    </ItemTemplate>
</asp:Repeater>

Then in your code behind:

protected static FormatActive(int active)
{
    return "Formated Active String...";
}

protected static FormatStatus(int status)
{
    return "Formated StatusString...";
}
烟雨凡馨 2024-12-20 11:44:21
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
         Active:   <asp:label id="lblActive" Text='<%# DataBinder.Eval(Container, "DataItem.Active")%>' runat="server" />        
     </div>
    </ItemTemplate>
</asp:Repeater>


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Do modifications here
        YourObjectName person = (YourObjectName)e.Item.DataItem;
        //you can now ref the object this row is bound to
        //example find a dom element
        Label lblActive= (Label)e.Item.FindControl("lblActive");

        if(person.Active == 2)
        {
            lblActive.Text = "This is great";
        }

    }
}
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
         Active:   <asp:label id="lblActive" Text='<%# DataBinder.Eval(Container, "DataItem.Active")%>' runat="server" />        
     </div>
    </ItemTemplate>
</asp:Repeater>


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Do modifications here
        YourObjectName person = (YourObjectName)e.Item.DataItem;
        //you can now ref the object this row is bound to
        //example find a dom element
        Label lblActive= (Label)e.Item.FindControl("lblActive");

        if(person.Active == 2)
        {
            lblActive.Text = "This is great";
        }

    }
}
九公里浅绿 2024-12-20 11:44:21

你可以这样做:

<%# (int)DataBinder.Eval(Container, "DataItem.Active") == 0 ? "Active" : "Inactive" %>

You could do something like:

<%# (int)DataBinder.Eval(Container, "DataItem.Active") == 0 ? "Active" : "Inactive" %>
不甘平庸 2024-12-20 11:44:21

无需使用 itemdatabound。只需在 itemtemplate 中添加一个方法即可使用 dataitem.active 作为参数进行转换。添加标签并执行以下操作:

Text='<%# String.Format("{0}",Conversion(Eval("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:

Text='<%# String.Format("{0}",Conversion(Eval("dataitem.active")))%>'

Conversion is then a method you leave in your code behind or utility class where you do the conversion.

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