asp.net - 如何切换中继器中 DIV 的可见性?

标签 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;
        }

最佳答案

您必须在绑定(bind)过程中使 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;
          }    
        }
    }

关于asp.net - 如何切换中继器中 DIV 的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10713580/

相关文章:

c# - 如何防止 Syncfusion ASP.Net Webforms Grid 中的过滤器文本框发送服务器端请求?

c# - 我正在尝试将复选框列表写入数据库,我错过了什么?

java - 一次将 CardLayout 中的多张卡片设置为 False

c# - 使用中继器控件不会触发 ItemCommand 事件

asp.net - ItemDataBound 中的 Xpath for Repeater

java - JTabbedPane:仅绘制第一个选项卡,第二个选项卡始终为空(新手Q)

jquery - 为什么我隐藏的 <tr> 没有真正隐藏?

ASP.Net 错误 - 无法将类型 'System.String' 的对象转换为类型 'System.Data.DataTable'

c# - 是否可以将 Crystal Reports 添加到针对 4.6 的 .NET Core 应用程序中

c# - 单击在 asp :Repeater 中呈现的 ascx 控件中的按钮时丢失值