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).
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> <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> <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> <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> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" Visible='<%# GetShowInsertButton() %>' CommandName="New" Text="New"></asp:LinkButton> <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.









November 17th, 2009 at 8:51 am
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;
December 4th, 2009 at 7:35 am
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
December 4th, 2009 at 7:43 am
Thanks Phil,
Mike’s also got a pretty good way to do it as mentioned above that may be even simpler.
-Peter
December 5th, 2009 at 7:44 am
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!
December 5th, 2009 at 8:22 am
Phil!=Peter;
It should be THANK YOU PETER
Forgive me..
January 19th, 2010 at 1:38 pm
But how do i set the click event ?
January 19th, 2010 at 1:45 pm
verbrauchsplan,
you can set the click event with onclick=.. or onclientclick=..