Skip to content

How to Disable Edit/Insert/New Buttons in DetailsView or GridView (ASP.NET 2.0+)

Updated: at 11:17 PM

So, this is kind of embarrassing, that it took me a while to figure this out.  I have not been doing pure asp.net server control programming for a while, but I figure since it took me a while, maybe there is someone else in the same boat.

So, you have a GridView or DetailsView that has standard “Edit” “Update” “New” type command buttons on them.  The way they get there is by having the declaration something like this:

 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
            AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" 
            DataSourceID="SqlDataSource1">
            <Fields>
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" 
                    SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="CampStartDate" HeaderText="CampStartDate" 
                    SortExpression="CampStartDate" />
                <asp:BoundField DataField="CampEndDate" HeaderText="CampEndDate" 
                    SortExpression="CampEndDate" />
                <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
</asp:DetailsView>

What you want is to say something like:

“I only want admin’s to be able to add new rows so  want to hide the “new” button based on that”

So, first thing is to convert the field to a template from the following menu.  You do this by using the little helper in the upper right hand corner of the detailsview (in design mode), chose Edit Fields, CommandField, then convert to template (as in the picture below).

image

Then, you will have something like this:

 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
            AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" 
            DataSourceID="SqlDataSource1">
            <Fields>
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" 
                    SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="CampStartDate" HeaderText="CampStartDate" 
                    SortExpression="CampStartDate" />
                <asp:BoundField DataField="CampEndDate" HeaderText="CampEndDate" 
                    SortExpression="CampEndDate" />
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Edit" Text="Edit"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="New" Text="New"></asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                            CommandName="Insert" Text="Insert"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </InsertItemTemplate>
                </asp:TemplateField>
            </Fields>
</asp:DetailsView>

Now, you have standard <asp:LinkButton> so you can call the visible property from your codebehind like this:

 <asp:TemplateField ShowHeader="False">
                     <ItemTemplate>
                         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" Visible='<%# GetShowEditButton() %>'
                             CommandName="Edit" Text="Edit"></asp:LinkButton>
                         &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" Visible='<%# GetShowInsertButton() %>'
                             CommandName="New" Text="New"></asp:LinkButton>
                         &nbsp;<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" 
                             CommandName="Delete" Text="Delete" Visible='<%# GetShowDeleteButton() %>'     ></asp:LinkButton>
                     </ItemTemplate>
</asp:TemplateField>

Then, in your codebehind, you have the following:

 protected bool GetShowEditButton()
    {
        return Roles.IsUserInRole("Admin");
    }
<span class="kwrd">protected</span> <span class="kwrd">bool</span> GetShowInsertButton()
{
    <span class="kwrd">return</span> Roles.IsUserInRole(<span class="str">&quot;Admin&quot;</span>);
}
<span class="kwrd">protected</span> <span class="kwrd">bool</span> GetShowDeleteButton()
{
    <span class="kwrd">return</span> Roles.IsUserInRole(<span class="str">&quot;Admin&quot;</span>);
}

}

Hope you find this before spending a bunch of time reading about more complicated solutions.