【问题标题】:Find div in repeater, and change it's style element在中继器中找到 div,并更改它的样式元素
【发布时间】:2013-04-16 21:53:43
【问题描述】:

我有一个如下所示的转发器对象

<asp:Repeater id="rptSpecialNotes" runat="server">
                    <headerTemplate><ol type="A"></HeaderTemplate>
                    <itemtemplate>
                                <li>
                                </li>
                        <asp:UpdatePanel ID="udpSpecialNotesRepeater" runat="server" UpdateMode="Always">
                            <ContentTemplate>

                                    <div id="specialNotes" name='<%# Eval("itemId") %>' runat="server">
                                        <asp:imagebutton runat="server"  width="20" class="floatLeft" id="specialNotesItem" imageurl='<%# Eval("ImageUrl") %>'  commandname="specialNotesImageChange" commandargument='<%# Eval("itemId") %>'></asp:imagebutton>
                                        <span class="subject"><p><%# eval("Subject") %></p></span>
                                        <br /><br />
                                        <p><%# Eval("AgendaNote")%></p>
                                        <br />
                                    </div>

                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID ="followAlongControl" EventName ="Tick" />
                            </Triggers>
                        </asp:UpdatePanel>
                        <asp:Repeater id="specialNotesAttachmentsRepeater" OnItemCommand="specialNotesAttachmentRepeater_ItemCommand" runat="server" datasource='<%# Container.DataItem.Row.GetChildRows("attachmentRelation") %>' >
                            <itemtemplate>
                                <br />
                                <a href='<%# Container.DataItem("attachmentPath") %>'><%# Container.DataItem("attachmentName") %></a>&nbsp;&nbsp;
                                </itemtemplate>
                        </asp:Repeater>
                    </itemtemplate>
                    <footerTemplate></ol></FooterTemplate>
                </asp:Repeater>

我想根据图像按钮的 imageurl 更改 div 'specialNotes' 的背景颜色。因此,如果选中,则 div 将为灰色,如果没有,则将其留空。

有没有办法在 VB 的代码隐藏中做到这一点?

【问题讨论】:

    标签: asp.net vb.net html asprepeater


    【解决方案1】:

    您不必在页面加载后“查找”div,您可以在它被绑定时执行此操作。我想当你说“如果它被选中”时,你指的是 imagebutton 作为一个复选框,并且你想根据图像的位置(路径)来改变颜色。所以我会在 div 中这样做:

    <div style='<%# doColor(Eval("ImageUrl")) %>'>
        Content in the div
    </div>
    

    然后在后面的代码中:

    Public Function doColor(img As Object) As String
        If img.ToString() = "MyPath" Then
            Return "background-color:red"
        Else
            Return "background-color:green"
        End If
    End Function
    

    这样,如果 ImageUrl 等于“MyPath”,则 div 的背景将为红色,否则为绿色。

    【讨论】:

    • 我无法正确安装它。我想我对我在哪里触发事件感到特别困惑?每次创建 div 时都会触发 doColor 吗?还是在我进行数据绑定的同一个地方触发它?您能否提供更多指导?我一定是忽略了什么……
    • 每次创建 div 时都会调用它。无论您在哪里绑定中继器,您都无需执行任何操作。
    • 完全按照我的需要工作。非常感谢!
    【解决方案2】:

    runat="server" 添加到您的 div 标签。然后您的代码在后面,您可以使用以下方式访问该 div:

    udpSpecialNotesRepeater.FindControl("div")

    关于您的 DataBound 事件。

    虽然在 jQuery 中做这件事要容易得多。您只需获取 div $("#specialNotes") 的 id 并使用 .attr 函数来更改其背景。

    【讨论】:

    • 但您无法获取“specialNotes”,因为它不是这样呈现的,而是在转发器数据绑定之后呈现为“specialNotes_01”或 w.e。我不知道我要提前抓住什么 div。除此之外,你不能在那里将 id 设置为 #eval("itemId"),因为它的语法不正确。
    • 这意味着它被呈现为 asp.net 控件。要访问一个asp.net 控件,可以使用“IdOfControl.ClientId”。在您的情况下,它是 specialNotes.ClientId。
    【解决方案3】:

    可以使用:

    Control divControl=e.Item.FindControl("divControl"); 
    

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 2019-08-13
      • 2020-05-16
      • 2012-02-08
      • 2016-06-03
      • 2016-05-18
      • 1970-01-01
      • 2022-08-12
      • 2014-03-04
      相关资源
      最近更新 更多