<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PeterKellner.net &#187; DetailsView</title>
	<atom:link href="http://peterkellner.net/category/detailsview/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net</link>
	<description>Microsoft Focused, JavaScript,HTML5 (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Fri, 11 May 2012 16:43:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to Disable Edit/Insert/New Buttons in DetailsView or GridView (ASP.NET 2.0+)</title>
		<link>http://peterkellner.net/2009/09/06/detailsview-gridview-aspnet-disable-new-edit-delete-buttons/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=detailsview-gridview-aspnet-disable-new-edit-delete-buttons</link>
		<comments>http://peterkellner.net/2009/09/06/detailsview-gridview-aspnet-disable-new-edit-delete-buttons/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 00:27:24 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 2.0]]></category>
		<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[DetailsView]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/09/06/detailsview-gridview-aspnet-disable-new-edit-delete-buttons/</guid>
		<description><![CDATA[So, this is kind of embarrassing, that it took me a while to figure this out.&#160; 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 [...]]]></description>
			<content:encoded><![CDATA[<p>So, this is kind of embarrassing, that it took me a while to figure this out.&#160; 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.</p>
<p>So, you have a GridView or <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview(VS.80).aspx">DetailsView</a> that has standard “Edit” “Update” “New” type command buttons on them.&#160; The way they get there is by having the declaration something like this:</p>
<p> <span id="more-352"></span>
<pre class="csharpcode"> &lt;asp:DetailsView ID=<span class="str">&quot;DetailsView1&quot;</span> runat=<span class="str">&quot;server&quot;</span> Height=<span class="str">&quot;50px&quot;</span> Width=<span class="str">&quot;125px&quot;</span>
            AllowPaging=<span class="str">&quot;True&quot;</span> AutoGenerateRows=<span class="str">&quot;False&quot;</span> DataKeyNames=<span class="str">&quot;Id&quot;</span>
            DataSourceID=<span class="str">&quot;SqlDataSource1&quot;</span>&gt;
            &lt;Fields&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;Id&quot;</span> HeaderText=<span class="str">&quot;Id&quot;</span> ReadOnly=<span class="str">&quot;True&quot;</span>
                    SortExpression=<span class="str">&quot;Id&quot;</span> /&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;Name&quot;</span> HeaderText=<span class="str">&quot;Name&quot;</span> SortExpression=<span class="str">&quot;Name&quot;</span> /&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;CampStartDate&quot;</span> HeaderText=<span class="str">&quot;CampStartDate&quot;</span>
                    SortExpression=<span class="str">&quot;CampStartDate&quot;</span> /&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;CampEndDate&quot;</span> HeaderText=<span class="str">&quot;CampEndDate&quot;</span>
                    SortExpression=<span class="str">&quot;CampEndDate&quot;</span> /&gt;
                &lt;asp:CommandField ShowEditButton=<span class="str">&quot;True&quot;</span> ShowInsertButton=<span class="str">&quot;True&quot;</span> /&gt;
            &lt;/Fields&gt;
&lt;/asp:DetailsView&gt;</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>What you want is to say something like:</p>
<p>“I only want admin’s to be able to add new rows so&#160; want to hide the “new” button based on that”</p>
<p>So, first thing is to convert the field to a template from the following menu.&#160; 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).</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/HowtoDisableEditInsertNewButtonsinDeta.0_F26A/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/HowtoDisableEditInsertNewButtonsinDeta.0_F26A/image_thumb.png" width="521" height="299" /></a></p>
<p>Then, you will have something like this:</p>
<pre class="csharpcode"> &lt;asp:DetailsView ID=<span class="str">&quot;DetailsView1&quot;</span> runat=<span class="str">&quot;server&quot;</span> Height=<span class="str">&quot;50px&quot;</span> Width=<span class="str">&quot;125px&quot;</span>
            AllowPaging=<span class="str">&quot;True&quot;</span> AutoGenerateRows=<span class="str">&quot;False&quot;</span> DataKeyNames=<span class="str">&quot;Id&quot;</span>
            DataSourceID=<span class="str">&quot;SqlDataSource1&quot;</span>&gt;
            &lt;Fields&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;Id&quot;</span> HeaderText=<span class="str">&quot;Id&quot;</span> ReadOnly=<span class="str">&quot;True&quot;</span>
                    SortExpression=<span class="str">&quot;Id&quot;</span> /&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;Name&quot;</span> HeaderText=<span class="str">&quot;Name&quot;</span> SortExpression=<span class="str">&quot;Name&quot;</span> /&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;CampStartDate&quot;</span> HeaderText=<span class="str">&quot;CampStartDate&quot;</span>
                    SortExpression=<span class="str">&quot;CampStartDate&quot;</span> /&gt;
                &lt;asp:BoundField DataField=<span class="str">&quot;CampEndDate&quot;</span> HeaderText=<span class="str">&quot;CampEndDate&quot;</span>
                    SortExpression=<span class="str">&quot;CampEndDate&quot;</span> /&gt;
                &lt;asp:TemplateField ShowHeader=<span class="str">&quot;False&quot;</span>&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:LinkButton ID=<span class="str">&quot;LinkButton1&quot;</span> runat=<span class="str">&quot;server&quot;</span> CausesValidation=<span class="str">&quot;False&quot;</span>
                            CommandName=<span class="str">&quot;Edit&quot;</span> Text=<span class="str">&quot;Edit&quot;</span>&gt;&lt;/asp:LinkButton&gt;
                        &amp;nbsp;&lt;asp:LinkButton ID=<span class="str">&quot;LinkButton2&quot;</span> runat=<span class="str">&quot;server&quot;</span> CausesValidation=<span class="str">&quot;False&quot;</span>
                            CommandName=<span class="str">&quot;New&quot;</span> Text=<span class="str">&quot;New&quot;</span>&gt;&lt;/asp:LinkButton&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:LinkButton ID=<span class="str">&quot;LinkButton1&quot;</span> runat=<span class="str">&quot;server&quot;</span> CausesValidation=<span class="str">&quot;True&quot;</span>
                            CommandName=<span class="str">&quot;Update&quot;</span> Text=<span class="str">&quot;Update&quot;</span>&gt;&lt;/asp:LinkButton&gt;
                        &amp;nbsp;&lt;asp:LinkButton ID=<span class="str">&quot;LinkButton2&quot;</span> runat=<span class="str">&quot;server&quot;</span> CausesValidation=<span class="str">&quot;False&quot;</span>
                            CommandName=<span class="str">&quot;Cancel&quot;</span> Text=<span class="str">&quot;Cancel&quot;</span>&gt;&lt;/asp:LinkButton&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;InsertItemTemplate&gt;
                        &lt;asp:LinkButton ID=<span class="str">&quot;LinkButton1&quot;</span> runat=<span class="str">&quot;server&quot;</span> CausesValidation=<span class="str">&quot;True&quot;</span>
                            CommandName=<span class="str">&quot;Insert&quot;</span> Text=<span class="str">&quot;Insert&quot;</span>&gt;&lt;/asp:LinkButton&gt;
                        &amp;nbsp;&lt;asp:LinkButton ID=<span class="str">&quot;LinkButton2&quot;</span> runat=<span class="str">&quot;server&quot;</span> CausesValidation=<span class="str">&quot;False&quot;</span>
                            CommandName=<span class="str">&quot;Cancel&quot;</span> Text=<span class="str">&quot;Cancel&quot;</span>&gt;&lt;/asp:LinkButton&gt;
                    &lt;/InsertItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
            &lt;/Fields&gt;
&lt;/asp:DetailsView&gt;</pre>
<p>
  </p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Now, you have standard &lt;asp:LinkButton&gt; so you can call the visible property from your codebehind like this:</p>
<pre class="csharpcode"> <span class="kwrd">&lt;</span><span class="html">asp:TemplateField</span> <span class="attr">ShowHeader</span><span class="kwrd">=&quot;False&quot;</span><span class="kwrd">&gt;</span>
                     <span class="kwrd">&lt;</span><span class="html">ItemTemplate</span><span class="kwrd">&gt;</span>
                         <span class="kwrd">&lt;</span><span class="html">asp:LinkButton</span> <span class="attr">ID</span><span class="kwrd">=&quot;LinkButton1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">CausesValidation</span><span class="kwrd">=&quot;False&quot;</span> <span class="attr">Visible</span><span class="kwrd">='&lt;%# GetShowEditButton() %&gt;'</span>
                             <span class="attr">CommandName</span><span class="kwrd">=&quot;Edit&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;Edit&quot;</span><span class="kwrd">&gt;&lt;/</span><span class="html">asp:LinkButton</span><span class="kwrd">&gt;</span>
                         <span class="attr">&amp;nbsp;</span><span class="kwrd">&lt;</span><span class="html">asp:LinkButton</span> <span class="attr">ID</span><span class="kwrd">=&quot;LinkButton2&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">CausesValidation</span><span class="kwrd">=&quot;False&quot;</span> <span class="attr">Visible</span><span class="kwrd">='&lt;%# GetShowInsertButton() %&gt;'</span>
                             <span class="attr">CommandName</span><span class="kwrd">=&quot;New&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;New&quot;</span><span class="kwrd">&gt;&lt;/</span><span class="html">asp:LinkButton</span><span class="kwrd">&gt;</span>
                         <span class="attr">&amp;nbsp;</span><span class="kwrd">&lt;</span><span class="html">asp:LinkButton</span> <span class="attr">ID</span><span class="kwrd">=&quot;LinkButton3&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">CausesValidation</span><span class="kwrd">=&quot;False&quot;</span>
                             <span class="attr">CommandName</span><span class="kwrd">=&quot;Delete&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;Delete&quot;</span> <span class="attr">Visible</span><span class="kwrd">='&lt;%# GetShowDeleteButton() %&gt;'</span>     <span class="kwrd">&gt;&lt;/</span><span class="html">asp:LinkButton</span><span class="kwrd">&gt;</span>
                     <span class="kwrd">&lt;/</span><span class="html">ItemTemplate</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">asp:TemplateField</span><span class="kwrd">&gt;</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Then, in your codebehind, you have the following:</p>
<pre class="csharpcode"> <span class="kwrd">protected</span> <span class="kwrd">bool</span> GetShowEditButton()
    {
        <span class="kwrd">return</span> Roles.IsUserInRole(<span class="str">&quot;Admin&quot;</span>);
    }

    <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>);
    }
}</pre>
<p>Hope you find this before spending a bunch of time reading about more complicated solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/09/06/detailsview-gridview-aspnet-disable-new-edit-delete-buttons/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 32/35 queries in 0.005 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: PetersBlogCDN.s3.amazonaws.com

Served from: peterkellner.net @ 2012-05-22 10:02:35 -->
