如何在转发器中切换DIV的可见性?

时间:2012-05-23 04:47:01

标签: asp.net repeater visible

我的转发器里面有一个DIV,如下所示:

    <asp:Repeater ID="topicView" runat="server" OnItemCommand="Delete_ItemCommand">
    <ItemTemplate>
    <table width="945px" cellpadding="0" cellspacing="0" border="0" class="post-table">
        <tr>
            <td colspan="2">
                <div class="post-info">
                    <div class="post-info-left"><%#DataBinder.Eval(Container.DataItem, "PostDate")%></div>
                    <div class="post-info-right">#<%#DataBinder.Eval(Container.DataItem, "PostID")%></div>
                    <div class="clear"></div>
                </div>
            </td>
        </tr>
        <tr>
            <td class="post-left">
                <p><strong><%#DataBinder.Eval(Container.DataItem, "Username")%></strong></p>
            </td>
            <td class="post-right">
                <p><%#DataBinder.Eval(Container.DataItem, "PostBody")%></p>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <div class="post-edit">
                    <p id="postEditAdmin" runat="server" visible="false"><a class="fancybox.iframe fancybox" href='editpost.aspx?postID=<%#DataBinder.Eval(Container.DataItem, "PostID")%>'>Edit</a> | <asp:LinkButton ID="deleteBtn" runat="server" Text="Delete" CommandName="Delete" OnClientClick="javascript:if(!confirm('Delete this information? this will delete permanently'))return false;" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "PostID")%>' /></p>
                    <p id="postEdit" runat="server" visible="true"><i>You must be an admin to be able to edit or delete a post</i></p>
                </div>
            </td>
        </tr>
    </table>
    </ItemTemplate>
</asp:Repeater>

有谁能告诉我如何将postEditAdmin.Visible设置为true,将postEdit.Visible设置为false?它没有在页面加载中被拾取。

另请注意,它会根据if语句进行更改。这就是我到目前为止所做的:

        if (Session["role"].ToString() == "2")
        {
            postEditAdmin.Visible = true;
            postEdit.Visible = false;
        }

2 个答案:

答案 0 :(得分:3)

您必须在绑定过程中使div可见或隐藏。将事件添加到转发器。

在aspx页面中,html side add

 <asp:Repeater ID="topicView" runat="server" OnItemCommand="Delete_ItemCommand" OnItemDataBound="topicView_ItemDataBound">

在文件添加后面的服务器端代码

    void topicView_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    {
         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         {
           // Show or hid div here
           System.Web.UI.HtmlControls.HtmlContainerControl postEditAdmin = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("postEditAdmin");

           System.Web.UI.HtmlControls.HtmlContainerControl postEdit= (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("postEdit");

          if (Session["role"].ToString() == "2")
          {
              postEditAdmin.Visible = true;
              postEdit.Visible = false;
          }    
        }
    }

答案 1 :(得分:0)

将postEditAdmin和postEdit部分包含在PlaceHolder控件中并设置其可见性:

<div class="post-edit">
    <asp:PlaceHolder id="postEditAdmin" runat="server" Visible="false">
         <p>...</p>
    </asp:PlaceHolder>

您还可以使用数据绑定来控制可见性,以避免代码隐藏:

<asp:PlaceHolder Visible="<%# Eval('PostEditAdminVisible') %>">