GridView - can field type be changed dynamically?

Last post 05-09-2008 7:52 AM by SynovialSivraj. 7 replies.

Sort Posts:

  • GridView - can field type be changed dynamically?

    05-09-2008, 6:24 AM

    Hi, I have a corporate asp.net application - access to application is restricted to members of a particular AD group on domain. There are two types of user - ones which have read-write access and ones which have read only access. I have database table containing usernames and column indicating if they are read-only user or not. I use a combination of Windows authentication and custom principal/identity classes to assign a "IsReadOnly" property to user's identity. So far so good.

    I am displaying records in GridView - for two of the columns in the grid, I need to display either a hyperlink field (for read-write users to click on) or a text field (for read-only users, so they can see values, but not click on). I need to figure out a way of swapping the hyperlink for a text field at runtime. The grid currently uses databound hyperlink fields, which I disable at runtime in the grid's RowDataBound event handler:

    // code to get user's identity omitted for brevity

    if (identity.IsReadOnly)
    {
     e.Row.Cells[2].Enabled = false;
     e.Row.Cells[3].Enabled = false;
    }

    but this disables the hyperlinks so that the text appears greyed out - not the effect I want. I want the text to appear normal - just not as a hyperlink. Any ideas? Have seen similar posts suggesting using a template field - is this the only way? If so, where can I find good examples?

  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 6:49 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 550

    You could try getting a reference to the HyperLink field and then set the NavigateUrl property to "", e.g.

    if (identity.IsReadOnly)
    {
        HyperLink lnk = e.Row.Cells[2].Controls[0] as HyperLink;
        if (lnk != null)
        {
            lnk.NavigateUrl = "";
        }
    }

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 6:50 AM
    Answer
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 2:22 PM
    • Tallinn, Estonia
    • Posts 281
    You should have one field with hyperlink and one usual text field. You have to show and hide these fields like this:
    grid.Columns[0].Visible = identity.IsReadOnly;
    grid.Columns[1].Visible = !identity.IsReadOnly;
    
    Just add this code before you bind GridView to data.
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 6:52 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 550

    Yep, DigiMortal's idea is definitely neater Smile 

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 7:04 AM
    • Loading...
    • ssnishad
    • Joined on 05-09-2008, 10:39 AM
    • Posts 1

    You can keep the hyper link column for the grid and by dynamically setting the Navigate Url property

    your problem can be solved.

    if (identity.IsReadOnly){
    CType(e.Item.Cells(3).Controls(0), HyperLink).NavigateUrl="";

    }

    else{

    //Set the the hyper link for the link here.

    }

  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 7:20 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 550

    ssnishad:

    if (identity.IsReadOnly){
    CType(e.Item.Cells(3).Controls(0), HyperLink).NavigateUrl="";

    }

    else{

    //Set the the hyper link for the link here.

    }

     

    Um, that's what I said but in your example you're mixing up c# and VB.Net so it wouldn't even compile. 

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 7:38 AM
    • Loading...
    • chandan.max
    • Joined on 03-28-2008, 6:37 AM
    • India
    • Posts 155

    "I want the text to appear normal - just not as a hyperlink "

    If u reallly wanted to not to use Hyperlink then use the CSS rather.

    If u need code then ask. 

    "Mark as Answer" on the post that helped you.

    Chandan,
    Imfinity India Pte Ltd.
  • Re: GridView - can field type be changed dynamically?

    05-09-2008, 7:52 AM

    I have tried several of the suggestions and would like to say that DigiMortal's answer turned out to be best.

    Whilst ramblor/ssnishad's answers seemed to be a lot simpler initially, setting the DataNavigateUrl property to an empty string still causes the text to be rendered in the same CSS style as a hyperlink, and trying to override the style added extra work. For a little more effort initially to create the extra two columns in the grid, DigiMortal's solution worked perfectly.

Page 1 of 1 (8 items)