Page view counter

Loginview with link, inside gridview

Last post 12-14-2005 5:33 AM by Adagio. 5 replies.

Sort Posts:

  • Loginview with link, inside gridview

    12-07-2005, 3:01 AM
    • Loading...
    • Adagio
    • Joined on 11-25-2005, 2:12 AM
    • Posts 52
    • Points 260

    I have a gridview that gets the data from a database, which works perfect, but in that gridview I also have two links (pictures) which both should open a page with a querystring (like: Page2.aspx?id=23) where the data also comes from the db. The problem is that these pictures should only be displayed when the logged in user is Admin. I can't seem to do it the same way as the hyperlinkfield in the gridview

    This is what I have:

    <asp:GridView ID="GridViewProducts" runat="server" AutoGenerateColumns="False" DataKeyNames="ProdID" DataSourceID="SqlDataSourceProducts" EmptyDataText="Der findes ingen produkter i valgte gruppe." AllowPaging="True" AllowSorting="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" PageSize="30" cssclass="list">

    <Columns>

    <asp:HyperLinkField DataNavigateUrlFields="ProdID" DataNavigateUrlFormatString="ProductView.aspx?ProdID={0}" DataTextField="Name" HeaderText="Produktnavn" NavigateUrl="~/MemberPages/ProductView.aspx" SortExpression="Name" />

    <asp:BoundField DataField="Description" HeaderText="Beskrivelse" SortExpression="Description" />

    <asp:TemplateField HeaderText="Stupid link" ShowHeader="False">

    <ItemTemplate>

    <asp:LoginView ID="loginivew" runat="server">

    <RoleGroups>

    <asp:RoleGroup Roles="Admin">

    <ContentTemplate>

    <asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/Images/DeleteIcon.png" OnClick="imgDelete_Click" />

    <asp:HyperLink ID="link" runat="server" NavigateUrl="~/Admin/EditProduct.aspx" ImageUrl="~/Images/EditIcon.png" ></asp:HyperLink>

    </ContentTemplate>

    </asp:RoleGroup>

    </RoleGroups>

    </asp:LoginView>

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

     

    Is there a better way to do it than how I'm trying to do it?

    Notice that I have tried both the imagebutton and hyperlink to do it, but none of them has the option I'm looking for

  • Re: Loginview with link, inside gridview

    12-12-2005, 4:32 AM
    • Loading...
    • Adagio
    • Joined on 11-25-2005, 2:12 AM
    • Posts 52
    • Points 260
    Nobody?
  • Re: Loginview with link, inside gridview

    12-12-2005, 5:10 AM
    • Loading...
    • Fredrik N
    • Joined on 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,333
    • Points 29,632
    • TrustedFriends-MVPs

    You can't do it with the HyperLinkField.

    What you can do is to add the HyperLinkField or modify it from the GridView's RowDataBound event. If you hook up to the GridView's RowDataBound event you can check if the user belongs to a specific role and based on the role you can modify the HyperLink.

    The following post on my blog can show you how to get access to a cell from the GridView's RowDataBound event. I hope it will give you some idea how to solve your problem (if you decide to do it with code): http://fredrik.nsquared2.com/viewpost.aspx?PostID=237

    /Fredrik Normén - fredrikn @ twitter

    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog
  • Re: Loginview with link, inside gridview

    12-14-2005, 3:05 AM
    • Loading...
    • Adagio
    • Joined on 11-25-2005, 2:12 AM
    • Posts 52
    • Points 260

    During RowDataBound I run this code:

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

    TableCell cell = e.Row.Cells[2];

    if (Profile.IsAnonymous == false)

    {

    if (Roles.IsUserInRole("Admin"))

    {

    cell.Text = "Delete";

    }

    else

    {

    cell.Visible = false;

    }}}

     

    And on the aspx page I added this column (and removed the other column

    <asp:HyperLinkField DataNavigateUrlFields="ProdID" DataNavigateUrlFormatString="ProductView.aspx?ProdID={0}" DataTextField="Name" HeaderText="Produktnavn" NavigateUrl="~/MemberPages/ProductView.aspx" />

    The checking if the logged in user is admin or not works fine, but I don't know how to remove the column, only the cell. Also I don't find any options to make it as a link because I can't access the hyperlinkfield in the cell, and there's no options in the cell to make it as a link

  • Re: Loginview with link, inside gridview

    12-14-2005, 4:08 AM
    • Loading...
    • Adagio
    • Joined on 11-25-2005, 2:12 AM
    • Posts 52
    • Points 260
    Where's the edit button? Hmmm... Hmm [^o)]

    I found out how to remove the column, but the link is still a problem
  • Re: Loginview with link, inside gridview

    12-14-2005, 5:33 AM
    • Loading...
    • Adagio
    • Joined on 11-25-2005, 2:12 AM
    • Posts 52
    • Points 260
    Edit button is defently needed, why was it removed? I didn't mean the link (I've done that), but how to make the link a picture. I can't find the needed options in the ImageField
Page 1 of 1 (6 items)