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

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");
    }

    protected bool GetShowInsertButton()
    {
        return Roles.IsUserInRole("Admin");
    }
    protected bool GetShowDeleteButton()
    {
        return Roles.IsUserInRole("Admin");
    }
}

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

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. If you don’t use the automatically generated buttons, setting CausesValidation=”True” on the LinkButton/Button for the Insert/Update commands will prevent the DetailsView from submitting if the validation fails.

  2. Dave,
    I’ve been messing around with how to disable the update until the validation passes on a textbox with no luck. I think this would best be handled with JavaScript.
    -Peter

  3. Sorry – that should have said “the ‘Insert’ button when in Insert mode, and the ‘Update’ button when in Edit mode?

    Either way, the intent is the same – don’t let the user even try to issue the Insert or Update until the data is valid.

    Currently I trap the Inserting and Updating events, revalidate and cancel as required, but I’d like to be neater.

  4. A quick question…

    Can this technique be applied in any way to selectively enable and disable the “Update” button when a DetailsView is in insert mode?

    I’d like to be able to disable Update based on the results of a validation method that checks some of the data…

    D.

  5. verbrauchsplan, you don’t set the click event, the event gets fired through the rowcommand as you would expect. (because it’s a linkbutton)

  6. Brilliant.

    One of those things where if you are trying to do it from a “windows forms mindset” gets very messy, but which need not be with the right mental toolset.

    Thank you.

  7. Easy. Simple. You are the man! Looked for hours and this is the best solution I could find.

  8. himanshu says:

    sir i just want to ask that
    when i click edit button my delete button will get disable…

  9. verbrauchsplan,

    you can set the click event with onclick=.. or onclientclick=..

  10. But how do i set the click event ?

  11. Phil!=Peter;
    It should be THANK YOU PETER
    Forgive me..

  12. Thanks very much!
    I was told to enhance the form so that submitting was possible even when Javascript was desabled,
    THIS POST HELPED A LOT!!
    just use the standard button ..beautiful..

    THANK YOU Phil!

  13. Thanks Phil,

    Mike’s also got a pretty good way to do it as mentioned above that may be even simpler.

    -Peter

  14. Phil Reid says:

    Peter,

    This a great, simple solution – Thankyou!

    It took me half a day of trawling through a number of more complicated solutions before I hit the jackpot with your post.

    Works like a dream! I’ll be bookmarking your site so I can start here in future.

    Kind Regards

    Phil

  15. Another approach…

    Have in your aspx for the <asp:DetailsView…

    AutoGenerateEditButton="True"

    Then in your codebehind, if you want rid of the button at any stage, chuck in a…

    dvwCheeses.AutoGenerateEditButton = false;

Your Comments

*

Protected with IP Blacklist CloudIP Blacklist Cloud

Follow

Get every new post delivered to your Inbox

Join other followers: