<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>crunchlife: Using LoginView on HyperLinkFields within a GridView</title>
    <link>http://crunchlife.com/articles/2008/08/13/using-loginview-on-hyperlinkfields-within-a-gridview</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Using LoginView on HyperLinkFields within a GridView</title>
      <description>&lt;p&gt;&lt;img src="/files/aspnet.gif" class="right"&gt;The title of this post is a bit misleading. Using a LoginView to manage the security of HyperLinkFields within a GridView does not work. There are, however, other means to achieve the same result.&lt;/p&gt;

&lt;p&gt;Using the GridView&amp;#8217;s OnRowDataBound event, I&amp;#8217;ve set my Cells containing HyperLinkFields, on DataRows with a RowIndex of -1, to an empty string if the user does not belong to the &amp;#8220;Administrators&amp;#8221; role. This hides my HyperLinkFields from underprivileged users and prevents elements of my GridView&amp;#8217;s header and footer from not appearing.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex != -1)
    {
        if (!Page.User.IsInRole(&amp;quot;Administrators&amp;quot;))        
            e.Row.Cells[0].Text = String.Empty;
    }           
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;*Update&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I just found a &lt;a href="http://forums.asp.net/t/943849.aspx" target="_blank"&gt;thread&lt;/a&gt; on the ASP.NET forums that covers the same problem. In their solution, the GridView&amp;#8217;s RowType is checked before setting the Cell&amp;#8217;s Visibility property to false. This makes more sense than relying on the RowIndex property to determine whether or not a DataRow&amp;#8217;s Cell should be hidden. In the method below, I&amp;#8217;ve integrated the DataControlRowType enumeration as suggested by the ASP.NET forums. Since setting the Visibility property of Cells containing HyperLinkFields caused my GridView headings to not line up properly, I decided to assign the Cell&amp;#8217;s Text property to String.Empty as in my previous example.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{                  
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (!Page.User.IsInRole(&amp;quot;Administrators&amp;quot;))
            e.Row.Cells[0].Text = String.Empty;
    }   
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 13 Aug 2008 12:45:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:95cccc69-ac5a-417a-aa72-8d64168cd1ab</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/08/13/using-loginview-on-hyperlinkfields-within-a-gridview</link>
      <category>Code Snippets</category>
      <category>ASPNET</category>
      <enclosure type="image/gif" length="3597" url="http://crunchlife.com/files/aspnet.gif"/>
    </item>
  </channel>
</rss>
