更改代码隐藏中重复器项目的样式?

发布于 2024-11-19 07:02:09 字数 92 浏览 3 评论 0原文

只是想知道是否有办法从代码隐藏中更改重复项目的样式/CSS。基本上我有一个页面的打印机友好版本,如果显示器是打印机友好的,我想为中继器中的每个项目添加底部边距。这可能吗?

just wondering if there is a way to change the style/css of repeateritems from the codebehind. Basically I have 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

树深时见影 2024-11-26 07:02:09

是的,可以在 中执行Repeater 的 ItemDataBound 事件。

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

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");
    }
孤云独去闲 2024-11-26 07:02:09

有多种应用样式的选项,请查看 Repeater 的 ItemDataBound 事件。

隐藏代码:

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>        

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>        
人海汹涌 2024-11-26 07:02:09

是的,你可以。你在重复什么? ?一个

只需从后面的代码更改 CssClass ... 例如:YourRepeatedItem.CssClass =“PrinterFriendly”。

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

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
孤星 2024-11-26 07:02:09

可以从代码隐藏中更改任何控件的样式。基本上,WebControl 类(所有 ASP.NET UI 控件的父级)具有 CssClass 属性和许多其他公开样式行为的属性。通过更改它们,您可以改变外观。

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.

回首观望 2024-11-26 07:02:09

您可能还想利用样式表中提供的“media”属性:http:// /www.javascriptkit.com/dhtmltutors/cssmedia.shtml

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

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

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