English 中文(简体)
某些物品的输出格式
原标题:Formatting the output of certain items bound to 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”事件这样做,但我 st不住我下一步应该采取的步骤,即检查我需要修改和修改的两个领域。

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Do modifications here
    }
}
最佳回答

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.

采用备选案文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>

然后,在您的“项目”活动中,你可以找到控制办法,并根据需要确定其价值。

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>

然后界定类似方法:

public string FormatActive(string input)
{
     //Format as required
     //Return formatted string
}
问题回答

我更喜欢采用标识中所要求的格式方法,而不是处理项目DataBound。

<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...";
}
<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";
        }

    }
}

你可以做这样的事情:

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

无需使用该数据。 仅添加一种方法,使转换成数据项目。 作为参数积极运行。 添加标签并做如下工作:

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签