1

我的中继器中有一个 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;
        }
4

2 回答 2

3

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

在aspx页面,html端添加

 <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;
          }    
        }
    }
于 2012-05-23T04:54:48.903 回答
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') %>">
于 2012-05-23T12:42:58.363 回答