3
Reply

How to set visible of buttons in a templatefield to false

Toh Zuan Yi

Toh Zuan Yi

Jul 25 2011 6:46 AM
3.3k
Hiiii once again all :) I have a minor problem again.... Does anyone know how to set visible of buttons in a template field to false if the userType(usertype table in database) is Admin? Example the gridview has 3 templatefields, and the last templatefield has 3 buttons, edit, delete and view. i want to set visible to false of Delete and Edit if the userType is Customer, and visible to true if userType is Admin. i have the sample code here

                        <asp:GridView ID="gvAnnouncement" runat="server"
                            AutoGenerateColumns="false"
                            CssClass="ProductListing"
                            Width="100%">
                            <RowStyle CssClass="ProductListingDetail" />
                            <HeaderStyle CssClass="ProductListingHeader" ForeColor="#ffffff" />
                            <EmptyDataRowStyle HorizontalAlign="Center" />
                            <EmptyDataTemplate>
                                No Announcements Available
                            </EmptyDataTemplate>
                            <Columns>
                                <asp:TemplateField HeaderText="Title">
                                    <ItemTemplate>
                                        <%# Eval("Title") %>
                                        <asp:HiddenField ID="hfAnnouncementID" runat="server" Value='<%#                                                 Eval("AnnouncementID") %>' />                                      
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Announcement">
                                    <ItemTemplate>
                                        <%# Eval("Announcements") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Date Created" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <%# string.Format("{0:dd-MMM-yyyy}", Eval("DateCreated"))%>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Button ID="btnView" runat="server" Text="View" CssClass="LoginButton"                                                 OnClick="btnViewAnnouncement_Click" Width="75px" />
                                        <asp:Button ID="btnEdit" runat="server" Text="Edit" CssClass="LoginButton"                                                 onclick="btnEditAnnouncement_Click" Width="75px" />
                                        <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="LoginButton"                                                 OnClick="btnDeleteAnnouncement_Click" Width="75px" />

                                    </ItemTemplate>
                                </asp:TemplateField>                                                  
                            </Columns>
                        </asp:GridView>          


i Highlighted the templatefield which i wanted to change, i would like to set visible to false for btnEdit and btnDelete if userType = customer. I need to do it in the Codebehind can anyone help me? i know i just need to declare some stuff before doing the if else but i dont know how to :( any help is appreciated so thanks in advance :)

Answers (3)