开发者

Change style of repeater item in codebehind?

just wondering if there is a way to change the style/css of repeateritems from the codebehind. Basically I ha开发者_如何学Gove a printer friendly version of a page and if the display is printer friendly i want to add a bottom margin to each item in the repeater. Is this possible?


Yes, it is possible to do within the ItemDataBound event of the Repeater.

   protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
        ((HtmlControl)e.Item.FindControl("SomeControl")).Attributes.Add("class", "cssStyle");
    }


There are several options to apply styles, hae a look at thee Repeater's ItemDataBound event.

codebehind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Dim tbl As New DataTable
        tbl.Columns.Add(New DataColumn())
        For i As Int32 = 1 To 10
            tbl.Rows.Add(tbl.NewRow)
            tbl.Rows(tbl.Rows.Count - 1)(0) = "Item " & i
        Next
        Me.Repeater1.DataSource = tbl
        Me.Repeater1.DataBind()
    End If
End Sub

Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Select Case e.Item.ItemType
        Case ListItemType.Item, ListItemType.AlternatingItem
            Dim dr = DirectCast(e.Item.DataItem, DataRowView)
            Dim Label1 = DirectCast(e.Item.FindControl("Label1"), Label)
            Label1.Text = dr(0)
            'use CssClass property'
            Label1.CssClass = "MyClass"
            'use Style property'
            Label1.Style.Add("color", "red")
            'use direct properties, for example'
            Label1.BackColor = Drawing.Color.Yellow
    End Select
End Sub

aspx:

<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate><table></HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" ></asp:Label>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>        


Yes you can. What are you repeating ? a <TR> ? a <DIV> ?

Just change the CssClass from code behind ... ex : YourRepeatedItem.CssClass = "PrinterFriendly".

For each ReaptedItem as repeaterItem in YourRepeater.items
    dim ItemToBeModified as htmlcontrol = RepeatedItem.findControl("ControlID")
    ItemToBeModified.CssClass = "PrinterFriendly"
Next


it is possible to change styles of any control from the codebehind. Basically the WebControl class ( the parent of all the asp.net UI controls ) is having the CssClass property and many other properties that expose the style behaviors. By changing them you can change the look.


You may also want to make use of the 'media' property that is available in style sheets: http://www.javascriptkit.com/dhtmltutors/cssmedia.shtml

<link rel="stylesheet" type="text/css" media="print" href="print.css">

OR

<style type="text/css">
@media print {
    .XXX{margin-bottom:5px;}
}
</style>
<asp:Label ID="Label1" CssClass="XXX" runat="server"></asp:Label>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜