更改gridview中按钮的可见性,以供不同用户使用

时间:2015-08-23 06:29:52

标签: c# asp.net gridview

我有一个评论的网格视图,显示登录用户发表的评论的删除按钮。我的意思是登录uder只能删除他自己的评论。 我已经尝试了以下但它没有显示任何用户的删除按钮。

 <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") %>'.....

提前致谢

1 个答案:

答案 0 :(得分:0)

有例子:

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)