locked
How do you set DataNavigateUrlFormatString programmatically? RRS feed

  • Question

  • User-1041323598 posted

    I have a GridView that contains a HyperLinkField.

    How do you set HyperLinkField.DataNavigateUrlFormatString programmatically?

    Friday, August 3, 2007 3:59 PM

Answers

  • User-2077437193 posted

    I want to set it row by row.  I'm trying to do this in the RowDataBound event, but I don't know how to reference the cell as a HyperLinkField.

     

    IIRC, you can do the following: in the event hander, you have a reference to the row, right? e.Row. Now that Row is made up of an indexed collection of Cells. So if the HyperLinkField is the left-most cell, you'd do e.Row.Cells[0] to reference the cell in the current row (use parentheses instead of brackets if you are using VB.NET). Now, for a HyperLinkField I believe the first control in the Control hierarchy is the HyperLink control itself, so do:

        HyperLink myLink = (HyperLink) e.Row.Cells[index].Controls[0];

    Alternatively, you can scrap the HyperLinkField and use a TemplateField. Then, drag a HyperLink control into the template and set its ID value. Once you've done that, you can programmatically reference the HyperLink control in the RowDataBound event handler using the following code:

       HyperLink myLink = (HyperLink) e.Row.FindControl("HyperLinkID");

    Happy Programming!
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2007 11:44 AM

All replies

  • User-2077437193 posted

    Are you wanting to set it programmatically for the entire column or on a row-by-row basis?

    I've not tested this, but you should be able to do something like the following (C#):

        ((HyperLinkColumn) GridViewID.Columns[index]).DataNavigateUrlFormatString = "...";

     

    You'd have to do this before the data is bound to the control.
     

    Friday, August 3, 2007 6:30 PM
  • User-1041323598 posted

    I want to set it row by row.  I'm trying to do this in the RowDataBound event, but I don't know how to reference the cell as a HyperLinkField.

    Monday, August 6, 2007 9:25 AM
  • User-2077437193 posted

    I want to set it row by row.  I'm trying to do this in the RowDataBound event, but I don't know how to reference the cell as a HyperLinkField.

     

    IIRC, you can do the following: in the event hander, you have a reference to the row, right? e.Row. Now that Row is made up of an indexed collection of Cells. So if the HyperLinkField is the left-most cell, you'd do e.Row.Cells[0] to reference the cell in the current row (use parentheses instead of brackets if you are using VB.NET). Now, for a HyperLinkField I believe the first control in the Control hierarchy is the HyperLink control itself, so do:

        HyperLink myLink = (HyperLink) e.Row.Cells[index].Controls[0];

    Alternatively, you can scrap the HyperLinkField and use a TemplateField. Then, drag a HyperLink control into the template and set its ID value. Once you've done that, you can programmatically reference the HyperLink control in the RowDataBound event handler using the following code:

       HyperLink myLink = (HyperLink) e.Row.FindControl("HyperLinkID");

    Happy Programming!
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2007 11:44 AM
  • User-1041323598 posted

    Thank you, Scott.  It worked!  I really appreciate your help.

     

    ...but how did you know that "e.Row.Cells[index].Controls[0]" would return a HyperLink instead of a HyperLinkField (or more importantly, where could I have found this info)?

    Monday, August 6, 2007 12:30 PM
  • User-2077437193 posted

    ...but how did you know that "e.Row.Cells[index].Controls[0]" would return a HyperLink instead of a HyperLinkField (or more importantly, where could I have found this info)?

     

    The DataGrid's HyperLinkColumn (which is constructed similarly) uses virtually the same code, so that's where I learned that bit, specifically. I learned about the structure of the DataGrid HyperLinkColumn just through trial and error. Poking around in the debugger. Exploring the Controls collection of e.Row.Cells[index] and so forth. I wish I had a better answer for you than that! [:S]

    In closing, I would recommend using the TemplateField approach over the HyperLinkField when needing to programmatically work with the resulting HyperLink cell on a row-by-row basis. The HyperLink syntax is less intuitive and is more brittle - if you move the position of the HyperLinkField explicitly or implicitly (by, say, adding new fields before the HyperLinkField), then the index will need to be updated in your code. If you forget to update it, kablamo, you'll have a nice runtime error next time you (or a customer) visits the page.
     

    Monday, August 6, 2007 1:04 PM
  • User1899120745 posted

    Hi Scott and ashwinanimal,

    I found a different programatic solution.  I have a form with a datagrid with a hyperlink field.  The contents of the hyperlink needed to be logically different based on the nature of the data for that row.  I needed two different urls going to 2 different forms.

    So I built a url column named LINK in the SQL statement that pulled data for the grid.  I tied the hyperlink field's DataNavigateUrlFields property to the field LINK.  Then to make the link display as a neat looking Work Order number that I needed rather than a long line of url text I tied the hyperlink field's DataTextField property to my WorkOrderNumber field.  Voila - programmatic hyperlink that displays the way I need. 

     No extra events to create.  Just build what I want in SQL for the row and bind it.

    Thanks, Tom O

    Wednesday, April 2, 2008 10:07 AM