为不同的用户改变gridview中按钮的可见性

本文关键字:按钮 可见性 gridview 用户 改变 | 更新日期: 2023-09-27 18:09:13

我有一个gridview的评论,其中显示了一个删除按钮的评论由登录用户。我的意思是,登录用户只能删除自己的评论。我试过下面的,但它没有显示任何用户的删除按钮。

 <asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="False" Width="540px" ShowHeader="False" CellPadding="4" ForeColor="Black" OnRowCommand="GridView1_RowCommand" DataKeyNames="CommentId">
                        <EmptyDataTemplate>
                            ---No comments on this event---
                        </EmptyDataTemplate>
                        <Columns>
                        <asp:TemplateField>
                        <ItemTemplate>
                            <table style="width:540px;background-color:white">
                                <tr>
                                    <td style="width:60px" rowspan="3">
                                        <asp:Image ID="imgUser" Width="50px" Height="50px" ImageUrl='<%# Bind("ProfilePicPath") %>' runat="server" style="border: 2px inset #C0C0C0" />
                                    </td>
                                    <td style="width:480px;border-bottom:solid 1px #999999">
                                        <asp:Label ID="lblCommentator" runat="server" Text='<%# Bind("FirstName") %>' style="color: #336699"></asp:Label>
                                        &nbsp;on
                                        <asp:Label ID="lblCommentTime" runat="server" Text='<%# Bind("CommentTime") %>' style="font-weight: 700"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width:480px;vertical-align:top">
                                        <asp:Label ID="lblComment" runat="server" Text='<%# Bind("Comment") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width:480px;vertical-align:top;text-align:right">
                                        &nbsp;<asp:HiddenField ID="hfUserId" Value='<%# Bind("UserId") %>' runat="server" />
                                        <asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# Session["UserId"] == Eval("UserId") %>' CommandArgument='<%#Eval("CommentId")%>' CommandName="Delete">Delete</asp:LinkButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        </asp:TemplateField>
                        </Columns>
                        </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SportsActiveConnectionString %>" SelectCommand="spExtractEventComments" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter Name="Eventid" Type="Int32" ControlID="lblEventId" />
                </SelectParameters>
            </asp:SqlDataSource>

正如你在上面看到的,我将当前用户(存储在会话中)与该评论的UserId进行比较。

  <asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# Session["UserId"] == Eval("UserId") %>'.....

Thanks in advance

为不同的用户改变gridview中按钮的可见性

有这样一个例子:

vb.net :
<asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# If(Session("UserId") = Eval("UserId"), "True", "False")%>'></asp:LinkButton>
c# (I think) :
<asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# If(Session["UserId"] == Eval("UserId").ToString() ? true : false)%>'></asp:LinkButton>

使用If检查值是否匹配(已测试和工作)

If(condition,do what You want if result is True, do what You want if result if False)