用Repeater绑定数据,将数据的前三条显示为红色

前台代码Code:

  < table >

   
< asp:Repeater runat = " server " ID = " rep " OnItemDataBound = " rep_ItemDataBound " >


< ItemTemplate >

< tr id = " row " runat = " server " >

< td >

<% # Eval( " title " ) %>

td >

tr >

ItemTemplate >

asp:Repeater >

table >

< br />

/* UI */

< ul >

< asp:Repeater runat = " server " ID = " rep1 " OnItemDataBound = " rep_ItemDataBound1 " >

< ItemTemplate >

< li runat = " server " id = " row1 " >

<% # Eval( " title " ) %> li >

ItemTemplate >

asp:Repeater >

ul >

后台代码Code:

// 当数据绑定格式为Table时,将类型转行成 HtmlTableRow

  
protected void rep_ItemDataBound( object sender, RepeaterItemEventArgs e)
    {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
if ((e.Item.ItemIndex + 1 ) == 1 || (e.Item.ItemIndex + 1 ) == 2 || (e.Item.ItemIndex + 1 ) == 3 )
            {
                ((HtmlTableRow)e.Item.FindControl(
" row " )).Attributes.Add( " style " , " color:red " );
            }
        }
    }
// 当数据绑定格式为UI时,将类型转行成 HtmlGenericControl

protected void rep_ItemDataBound1( object sender, RepeaterItemEventArgs e)

    {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

if ((e.Item.ItemIndex + 1 ) == 1 || (e.Item.ItemIndex + 1 ) == 2 || (e.Item.ItemIndex + 1 ) == 3 )

            {

                ((HtmlGenericControl)e.Item.FindControl(
" row1 " )).Attributes.Add( " style " , " color:red " );

            }
        }
    }

你可能感兴趣的:(SQL)